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');    

「Exit を解決できません」のようなエラーが表示されます。y私の目的は、ユーザーがその質問に入力するまでループすることです。

4

4 に答える 4

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 で文字列を比較するにはどうすればよいですか? を参照してください。その理由のために。!=オブジェクトを比較し、それらが2つの異なるオブジェクトであると判断するため、基本的には常にtrueになる可能性があります。

于 2013-01-04T03:34:56.200 に答える