簡體   English   中英

掃描儀和if語句

[英]Scanner and if statement

我是Java的新手,我想創建一個簡單的程序,允許用戶在3個選項(t,r和q)之間進行選擇。 q表示退出。 我希望程序繼續提示他們選擇其他選項,直到他們選擇退出為止。 下面是我的代碼,它不起作用。 我得到q,r和t需要解析為變量的錯誤,但是當我將它們設置為字符串時,它仍然不起作用。 任何幫助將不勝感激。

Scanner input3= new Scanner(System.in);
String choice;
boolean valid;

do 
{
    System.out.println("Please pick an option. t, r or q");
    choice= input3.next();

    if(choice==t)
    {
        System.out.println("You chose triangle");
        valid=true;             
    }
    else if(choice==r)
    {
        System.out.println("You chose rectangle");
        valid=true;
    }
    else if (choice==q)
    {
        System.out.println("You chose to quit.");
        valid=false;
    }
    else
    {
        System.out.println("You chose wrong.");
        valid=true;
    }

}
while( valid==true);

您應該將它們用引號引起來"因為choice是字符串,您必須將其與字符串進行比較,例如:

do {
    System.out.println("Please pick an option. t, r or q");
    choice= input3.next();

    if(choice=="t"){
         System.out.println("You chose triangle");
         valid=true;             
    }

    else if(choice=="r"){
         System.out.println("You chose rectangle");
         valid=true;
    }

    else if (choice=="q"){
         System.out.println("You chose to quit.");
         valid=false;
    }
    else{
         System.out.println("You chose wrong.");
         valid=true;
    }

}
while( valid==true);

您可以使用equals()代替==來比較兩個字符串:

do {
    System.out.println("Please pick an option. t, r or q");
    choice= input3.next();

    if(choice.equals("t")){
         System.out.println("You chose triangle");
         valid=true;             
    }

    else if(choice.equals("r")){
         System.out.println("You chose rectangle");
         valid=true;
    }

    else if (choice.equals("q")){
         System.out.println("You chose to quit.");
         valid=false;
    }
    else{
         System.out.println("You chose wrong.");
         valid=true;
    }

}
while( valid==true);

希望這可以幫助。

在網上查看初學者教程(以及以下SO問題: Java String.equals與==

首先需要將t,r和q設置為String變量。

例如:

String choice;
String t = "t";
String r = "r";
String q = "q";

您還應該使用equals方法實現字符串相等。

例如:

choice.equals(t);
import java.util.Scanner;
public class TestClass {
    public static void main(String args[] ) throws Exception {
        Scanner input3= new Scanner(System.in);
        String choice;
        boolean valid;


             do {
                 System.out.println("Please pick an option. t, r or q");
                 choice= input3.next();
             if(choice.equals("t")){

                 System.out.println("You chose triangle");
                 valid=true;             
             }

             else if(choice.equals("r")){
                 System.out.println("You chose rectangle");
                 valid=true;
             }

             else if (choice.equals("q")){
                 System.out.println("You chose to quit.");
                 valid=false;
             }
             else{
                 System.out.println("You chose wrong.");
                 valid=true;
             }

             }
             while( valid==true);


    }
}

請享用!!!

字符串需要與函數string1.equals(string2)進行比較。 將所有if和if else語句更改為如下所示:

choice.equals("t");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM