如何将用户输入到for循环中。并使程序循环直到用户输入退出。这是我的代码:
System.out.println("what is your full name?" );
Name = scan.nextLine();
String newString = Name.replaceAll(" ", "");
System.out.println("please enter " + newString.length() + " numbers and the total will be calculated");
if (newString.length() == 3) {
for(int count = 1; count <= newString.length(); count++)
System.out.println( "Number " + count + ":" + );
Num1 = scan.nextInt();
Num2 = scan.nextInt();
Num3 = scan.nextInt();
System.out.println("total is " + (Num1 + Num2 + Num3));
}
在for循环内尝试一下:
boolean flag =true;
while (flag){
String input = scan.nextLine();
if (input == "quit"){
flag =false;
} else {
//Your code here
}