用Java生成乘法问题

时间:2017-04-01 16:11:39

标签: java loops if-statement for-loop while-loop

代码应输出Farenheit和Celsius的表格

public static void main(String[] args) {
    System.out.println("Fahrenheit\tCelsius");
    System.out.println("=======================");
     for(int temp = -45; temp <= 120; temp += 5) //for(int i = 0; i <= 100; i+= 10)
        {
            System.out.printf("%5d       |",          temp);
            double sum = (temp + (9.0/5.0)) * 32;   
            System.out.printf("%5d", (int)sum );
            System.out.println();

3 个答案:

答案 0 :(得分:1)

您需要添加do while()循环才能继续提问,例如:

static Scanner input;
static Scanner scanner;
static String question;

public static void main(String[] args) {
    do {
        int number1 = (int) (Math.random() * 10);
        int number2 = (int) (Math.random() * 10);
        input = new Scanner(System.in);
        System.out.print("What is " + number1 + " * " + number2 + "? ");
        int answer = input.nextInt();
        while ((number1 * number2) != answer) {
            System.out.print("Incorrect. Please try again. What is "
                    + number1 + " * " + number2 + "? ");
            answer = input.nextInt();
        }
        if ((number1 * number2) == answer) {
            System.out.println("Correct. Nice work!");
            System.out.println("Want more questions yes or no? ");
            scanner = new Scanner(System.in);
            question = scanner.next();
        }
    } while (question.toLowerCase().equals("yes") ||
            question.toLowerCase().equals("y"));
}

答案 1 :(得分:0)

最简单的方法是循环播放。示例:

do{
    // What you want to repeat and make sure to change have way to get out of loop like this:
    System.out.print("Want more questions yes or no? ");
    question = scanner.next();
}while(question.equals("yes") || question.equals("y"));

答案 2 :(得分:0)

您的代码中存在一些错误,但我建议您从简单的读取输入的情况开始,直到用户输入“n”,而不是为您提供完整的解决问题的解决方案:

    String input = "";
    Scanner scanner = new Scanner(System. in); 

    while (!input.equals("n")) {
        System.out.println("continue y/n?");
        input = scanner.nextLine();
    }

在while循环体内添加随机整数的生成,检查答案等。在比较两个字符串时,请务必使用equals而不是==