如何读取用户输入以在java中重新运行程序?

时间:2015-10-05 05:59:32

标签: java while-loop user-input

我正在创建一个程序,提示用户输入正整数。程序接受整数,直到输入-1。用户输入another = input.nextLine()后,输入的整数和整数之和将显示。如果用户尝试输入负数,程序会告诉用户它是无效输入并输入正整数。我遇到的问题是在程序的最后。它不允许我输入y(是)或(否),即使它应该让我,因为我特意添加了import java.util.Scanner; public class InputSum { public static void main (String[] args) { int integer = 0; int sum = 0; int count = 0; String string = " "; String another = "y"; Scanner input = new Scanner(System.in); // scanner object while (another.equalsIgnoreCase("y")) { while (integer != -1){ count = count + 1; System.out.print ("Enter a positive integer (-1 to exit): "); integer = input.nextInt(); while (integer < -1){ System.out.print ("Invalid input was entered. Enter a positive integer (-1 to exit) : "); integer = input.nextInt(); } if (integer == -1) continue; sum = sum + integer; string = string + integer + ", "; } if (count == 1){ System.out.println ("\n" + "No values were entered"); System.out.println ("The Sum: " + sum); } else System.out.println ("\n" + "Entered value: " + string.substring(0, string.length()-2) + "\n" + "The Sum: " + "\t" + sum); System.out.print ("Run the program again (y/n)?: "); another = input.nextLine(); } } } ;。它只是完成了程序。我如何拥有它以便读取 y(是) n(no)的用户输入并让用户输入另一组整数?

SELECT train.id,
    train.class_id,
    train.type_id,
    train.m_year,
    train_type.type,
    train_type.avarage_speed,
    train_class.class,
    train_class.capacity
FROM train
    INNER JOIN train_class
ON  train.class_id = train_class.id
    AND train.type_id = train_type.id

0 个答案:

没有答案