【问题标题】:Check if user entered both a first and last name检查用户是否输入了名字和姓氏
【发布时间】:2015-09-25 05:20:06
【问题描述】:

我正在制作一个程序,为输入的姓名提供随机彩票号码。但问题是我必须确保用户输入了名字和姓氏。我正在使用一种在用户输入中查找空间的方法,然后从该数据创建子字符串,但我一直在我的 for 循环下收到错误“不兼容的类型”。任何帮助将不胜感激!

enter code here
import java.util.Scanner;      //Import scanner class
import java.util.Random;       //Import random number generator
import java.io.*;              //Import PrintWriter

public class Lab4ZinkovskyFl   //Program that lets user enter a name and generates random lottery numbers for that name
{
public static void main (String[] args) throws IOException
{
Scanner keyboard = new Scanner (System.in);

Random randomNumbers = new Random();

String again = "y";                      //Control the loop
int r1 = randomNumbers.nextInt(100)+ 1;  //*******************
int r2 = randomNumbers.nextInt(100)+ 1;  //*   Random lottery  
int r3 = randomNumbers.nextInt(100)+ 1;  //*   numbers for    
int r4 = randomNumbers.nextInt(100)+ 1;  //*   program        
int r5 = randomNumbers.nextInt(100)+ 1;  //******************* 

    while (again.equalsIgnoreCase ("y"))  // Allows the user to continue the loop
    {
        System.out.println ("Please enter first and last name to enter the lottery.");
        String fullName = keyboard.nextLine();

        boolean space = false;  // Checks for first and last name       

        for (int i = 0; i < fullName.length(); i++)
        {       
            if (fullName.indexOf(i) == " ")
            {
                space = true;
                spaceIndex = i;
            }
            else 
            {
                System.out.println ("Error, please enter both first and last name to continue.");
            }

        }
    String firstName = fullName.substring (0, spaceIndex);
    String lastName = fullName.substring (spaceIndex, fullName.length());

    System.out.println (lastName + ", " + firstName + ": " + r1 + ", " + r2 + ", " + r3 + ", " + r4 + ", " + r5);


    System.out.println ("Run the lottery again? (y=yes)");
    again = keyboard.nextLine();
    }       
  }
}

【问题讨论】:

    标签: java string int indexof


    【解决方案1】:

    你可以用“”分割用户输入,像这样:

    String[] names = fullName.split(" ");
    

    然后你创建一个方法来在用户输入全名时返回 true。

    for (int i = 0 ; i < names.length ; i++) {
        if (names[i].trim().equals("")) {
            names[i] = null;
        }
    }
    int elementsWithText = 0;
    for (int i = 0 ; i < names.length ; i++) {
        if (names[i] != null) {
            elementsWithText++;
        }
    }
    
    return elementsWithText == 2;
    

    类似的东西。希望你明白我在做什么。如果你不知道方法调用在做什么,它们都来自String。这是文档:

    http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

    【讨论】:

      【解决方案2】:

      indexOf() 将 char 作为输入(在您的情况下)。把 i 改成 " "(空格)

      【讨论】:

        【解决方案3】:

        你需要这样写

          if (fullName.indexOf(" ") == -1) 
           {
             System.out.println ("Error, please enter both first and last name to continue.");
           }
           else 
           {
             space = true;
             spaceIndex = i;
           }
        

        但是你为什么选择for循环呢?

        @sweeper 给出了最好的解决方案。

        【讨论】:

          猜你喜欢
          • 2020-02-01
          • 2021-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-22
          相关资源
          最近更新 更多