我试图弄清楚如何使用带有循环的scanner类。我不能使用break作为我的代码的一部分。我不确定我有多接近,但是我们非常感激任何帮助。
import java.util.Scanner;
public static void main(String[] args) {
Scanner i = new Scanner(System.in);
System.out.print("Please enter a number between 1 and 10: ");
int number = i.nextInt();
boolean value = number>=1 && number <=10;
while (number >=1 && number <=10){
System.out.println ("Good Job!");
}
if (number <=1 && number >=10 )
continue;
{System.out.print ("You have entered an incorrect number. Please try again");
System.out.println("Please try again:");
Scanner r = new Scanner(System.in);
return;
}
答案 0 :(得分:0)
可以像这样使用Scanner类:
import java.util.Scanner;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// now loop while the scanner is available
while(scanner.hasNext()) {
// print out every single number input from standard input
System.out.println("Scanner inputed from stdin is " + Integer.toString(scanner.nextInt());
}
}
答案 1 :(得分:0)
你可以这样做:
import java.util.Scanner;
import java.util.Stack;
public class ScannerDemo {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String next = scan.next();
if("quit".equals(next)) break;
System.out.println("Element: " + next);
}
scan.close();
}
}
按quit
退出。