3
import java.util.Scanner;
import java.util.*;

public class MultipicationTable{

    public static void main(String[] args)
    {
        // Initialising selection variable to 0 
        int selection = 0;
        int MultiValue = 0;
        int UserValue = 0;

        // Initializing the count for loop
        int i;


        // Initializing Random1 and Random2 to get random values
        int Random1;
        int Random2;


        // Creating new Scanner
        Scanner input = new Scanner(System.in);

        do{

        // Menu to select type of multipication
        System.out.println("Please select your option");
        System.out.println("1: Random Multipication table");
        System.out.println("2: Give your own numbers");

        // Getting the input from the above menu
        selection = input.nextInt();

        switch (selection)
        {
            case 1:


                Random1 = (int)(Math.random() * 1000);
                Random2 = (int)(Math.random() * 1000);

                Random1 = Random1/10;

                System.out.println("You Random Value to pultiply is " + Random1);


                System.out.println("How long do you want? 2 - 100 ");


                MultiValue = input.nextInt();


                for (i = 1; i <= MultiValue; i++ )
                {
                    System.out.println("Multipication of " + Random1 + " * " + i + " is: " + Random1 * i);
                }



            case 2:

                System.out.println("What is your Number? ");
                UserValue = input.nextInt();

                System.out.println("How long do you want to multiply? ");
                MultiValue = input.nextInt();

                for (i = 1; i <= MultiValue; i++ )
                {
                    System.out.println("Multipication of " + UserValue + " * " + i + " is: " + UserValue * i);
                }



        }

        System.out.println("Would you like to exit? ");
        String Exit = input.nextLine();

        }while(Exit != 'y');    

    }



}

我认为我的错误出在这部分代码上。

    System.out.println("Would you like to exit? ");
    String Exit = input.nextLine();

}while(Exit != 'y');    

我收到类似“无法解决退出”的错误。我的目标是循环直到用户输入y该问题。

4

4 回答 4

5

首先,whileordo...while循环中的条件在外部范围内。这意味着在do...while检查条件时,在块中声明的任何变量都不再在范围内。为了解决这个问题,你需要在循环exit外声明你的变量:do...while

String Exit = null;

do {
    // do something
    Exit = input.nextLine();
} while (Exit != 'y');

但是,此代码仍将无法编译,因为Exit它被声明为 aString但您正在将它与'y'which is a进行比较char。字符串文字总是用双引号括起来,"y"字符串也是如此。

现在将条件更改为Exit != "y"将编译,但它不会按预期运行。您需要使用该方法比较Strings 。equals()这意味着条件应该是!Exit.equals("y")。将它放在while条件中应该可以解决循环的问题。

或者,如果您想检查单词“yes”或变体,您可以使用while(Exit.charAt(0) != 'y');. 这将检查第一个字符Exit是否是 'y' 字符。

于 2013-01-04T03:40:05.843 回答
1

String Exit必须在循环外声明

String Exit =null;
do {
//body
Exit= input.next(); // or input.nextLine();
} while(!Exit.equals("y"));

如 Kartik 所述编辑:应该使用 equals 来比较字符串。和 == 检查两个变量是否引用同一个对象。

于 2013-01-04T03:32:00.480 回答
0

您的第一个问题是Exit@Subin 涵盖的范围。您可能也想使用}while(!Exit.equals("y");

您可以查看如何比较 Java 中的字符串?由于这个原因。基本上你!=可能总是正确的,因为它比较对象,因此会决定它们是两个不同的对象。

于 2013-01-04T03:34:56.200 回答
-1
    System.out.println("Would you like to exit? ");
    String Exit = input.nextLine();

}while(!Exit.equals('y');
于 2013-01-04T03:39:21.767 回答