如何使此代码拒绝q

时间:2016-10-14 02:53:22

标签: java bluej

我还有点新意,所以我将包含我所有的java代码,然后解释我在寻找什么。

import java.util.Scanner;
public class Part_I{
    public static Scanner input = new Scanner(System.in);
    public static String strInfo;
    public static int number;
    public static void main(String[] args){
        String presidents[][] = {
        {"1 ","George"," ","Washington"," (1789-1797) ","John Adams"},
        {"2 ","John"," ","Adams"," (1797-1801) ","Thomas Jefferson"},
        {"3 ","Thomas"," ","Jefferson"," (1801-1809) ","Aaron Burr"},
        {"4 ","James"," ","Madison"," (1809-1817) ","George Clinton"},
        {"5 ","James"," ","Monroe"," (1817-1825) ","Daniel D. Tompkins"},
        {"6 ","John"," Quincy ","Adams"," (1825-1829) ","John C. Calhoun"},
        {"7 ","Andrew"," ","Jackson"," (1829-1837) ","John C. Calhoun"},
        {"8 ","Martin"," Van ","Buren"," (1837-1841) ","Richard M. Johnson"},
        {"9 ","William"," Henry ","Harrison"," (1841) ","John Tyler"},
        {"10 ","John"," ","Tyler"," (1841-1845) ","None"},
        {"11 ","James"," K. ","Polk"," (1845-1849) ","George M. Dallas"},
        {"12 ","Zachary"," ","Taylor"," (1849-1850) ","Millard Fillmore"},
        {"13 ","Millard"," ","Fillmore"," (1850-1853) ","None"},
        {"14 ","Franklin"," ","Pierce"," (1853-1857) ","William King"},
        {"15 ","James"," ","Buchanan"," (1857-1861) ","John C. Breckinridge"},
        {"16 ","Abraham"," ","Lincoln"," (1861-1865) ","Hannibal Hamlin"},
        {"17 ","Andrew"," ","Johnson"," (1865-1869) ","None"},
        {"18 ","Ulysses"," S. ","Grant"," (1869-1877) ","Schuyler Colfax"},
        {"19 ","Rutherford"," B. ","Hayes"," (1877-1881) ","William Wheeler"},
        {"20 ","James"," A. ","Garfield"," (1881) ","Chester Arthur"},
        {"21 ","Chester"," ","Arthur"," (1881-1885) ","None"},
        {"22 ","Grover"," ","Cleveland"," (1885-1889) ","Thomas Hendricks"},
        {"23 ","Benjamin"," ","Harrison"," (1889-1893) ","Levi P. Morton"},
        {"24 ","Grover"," ","Cleveland"," (1893-1897) ","Adlai E. Stevenson"},
        {"25 ","William"," ","McKinley"," (1897-1901) ","Garret Hobart"},
        {"26 ","Theodore"," ","Roosevelt"," (1901-1909) ","None"},
        {"27 ","William"," Howard ","Taft"," (1909-1913) ","James S. Sherman"},
        {"28 ","Woodrow"," ","Wilson"," (1913-1921) ","Thomas R. Marshall"},
        {"29 ","Warren"," G. ","Harding"," (1921-1923) ","Calvin Coolidge"},
        {"30 ","Calvin"," ","Coolidge"," (1923-1929) ","None"},
        {"31 ","Herbert"," ","Hoover"," (1929-1933) ","Charles Curtis"},
        {"32 ","Franklin"," D. ","Roosevelt"," (1933-1945) ","John Nance Garner"},
        {"33 ","Harry"," S. ","Truman"," (1945-1953) ","None"},
        {"34 ","Dwight"," D. ","Eisenhower"," (1953-1961) ","Richard Nixon"},
        {"35 ","John"," F. ","Kennedy"," (1961-1963) ","Lyndon B. Johnson"},
        {"36 ","Lyndon"," B. ","Johnson"," (1963-1969) ","None"},
        {"37 ","Richard"," ","Nixon"," (1969-1974) ","Spiro Agnew"},
        {"38 ","Gerald"," ","Ford"," (1974-1977) ","Nelson Rockefeller"},
        {"39 ","Jimmy"," ","Carter"," (1977-1981) ","Walter Mondale"},
        {"40 ","Ronald"," ","Reagan"," (1981-1989) ","George Bush"},
        {"41 ","George"," ","Bush"," (1989-1993) ","Dan Quayle"},
        {"42 ","Bill"," ","Clinton"," (1993-2001) ","Al Gore"},
        {"43 ","George"," W. ","Bush"," (2001-2009) ","Dick Cheney"},
        {"44 ","Barack"," ","Obama"," (2009-2017) ","Joe Biden"},
        };
        System.out.println("This will display the President and VP of the United States based on the number you provide.");
        System.out.println("Please enter a number between 1 and 44 to see information or q to quit: ");
        strInfo = input.nextLine();
        while(strInfo != "q"){
            if(isInteger(strInfo)){    
                number = Integer.parseInt(strInfo);
                if (number >= 1 && number <=44){
                    System.out.println();
                    System.out.println(presidents[number-1][0] + "President " + presidents[number-1][1] + presidents[number-1][2] + presidents[number-1][3] + presidents[number-1][4] + "Vice President " + presidents[number-1][5]);
                    System.out.println();
                    System.out.println("Please enter a number between 1 and 44 to see information or q to quit: ");
                    strInfo = input.nextLine();
                }else{
                    System.out.println();
                    System.out.println("Wrong Input! Please enter number 1-44 or q to quit.");
                    strInfo = input.nextLine();
                }
            }else{
                System.out.println();
                System.out.println("This program has been terminated. Good Bye!");
                System.exit(0);
            }
        }
    }
    public static boolean isInteger(String strInfo){
        if (strInfo == null) {
            return false;
        }
        int length = strInfo.length();
        if (length == 0) {
            return false;
        }
        int i = 0;
        if (strInfo.charAt(0) == '-') {
            if (length == 1) {
                return false;
            }
            i = 1;
        }
        for (; i < length; i++) {
            char c = strInfo.charAt(i);
            if (c < '0' || c > '9') {
                return false;
            }
        }
        return true;
    }
}

我主要担心的是while循环。

while(strInfo != "q"){
    if(isInteger(strInfo)){    
        number = Integer.parseInt(strInfo);
        if (number >= 1 && number <=44){
            System.out.println();
            System.out.println(presidents[number-1][0] + "President " + presidents[number-1][1] + presidents[number-1][2] + presidents[number-1][3] + presidents[number-1][4] + "Vice President " + presidents[number-1][5]);
            System.out.println();
            System.out.println("Please enter a number between 1 and 44 to see information or q to quit: ");
            strInfo = input.nextLine();
        }else{
            System.out.println();
            System.out.println("Wrong Input! Please enter number 1-44 or q to quit.");
            strInfo = input.nextLine();
        }
    }else{
        System.out.println();
        System.out.println("This program has been terminated. Good Bye!");
        System.exit(0);
    }
  }
}

我想这样做,除了可以转换为int或“q”之外的任何字符串都会输入错误的输入并让你输入另一个字符串值。现在,任何字符串都会使程序终止。我应该在while循环中更改什么以及如何更改它或者它应该是什么样的,以便如果字符串输入不是q或可转换为int将导致错误的输入显示并再次请求输入?

3 个答案:

答案 0 :(得分:0)

你不应该使用像#34; =&#34;这样的普通算子来检查字符串的相等性。和&#34;!=&#34;。使用String .equals()方法。

所以你的第一行是

while(!strInfo.equals("q"))

更多信息:

http://www.leepoint.net/data/expressions/22compareobjects.html

答案 1 :(得分:0)

这将帮助您实现您想要的目标

 while (!strInfo.equals("q")) {
            if (isInteger(strInfo)) {
                number = Integer.parseInt(strInfo);
                if (number >= 1 && number <= 44) {
                    System.out.println();
                    System.out.println(presidents[number - 1][0] + "President " + presidents[number - 1][1] + presidents[number - 1][2] + presidents[number - 1][3] + presidents[number - 1][4] + "Vice President " + presidents[number - 1][5]);
                    System.out.println();
                    System.out.println("Please enter a number between 1 and 44 to see information or q to quit: ");
                    strInfo = input.nextLine();
                } else {
                    System.out.println();
                    System.out.println("Wrong Input! Please enter number 1-44 or q to quit.");
                    strInfo = input.nextLine();
                }
            } else {
                System.out.println();
                System.out.println("Wrong Input! Please enter number 1-44 or q to quit.");
                strInfo = input.nextLine();
            }
        }
        System.out.println();
        System.out.println("This program has been terminated. Good Bye!");
        System.exit(0);

答案 2 :(得分:0)

您的代码无法工作的原因是因为您尝试使用==运算符(仅在两个引用指向同一对象时进行比较)来比较两个字符串的内容是否相等。 ==运算符不会比较两个字符串的内容。

为了使您的代码有效,您需要使用等于来比较两个字符串的内容,如下所示:

while(!strInfo.equals("q"))

现在让我们尝试深入研究您的代码无法正常工作的原因。为此,我们需要了解==&amp;的基本区别。等于

==运算符用于比较两边的引用是否指向同一个对象(基本上你可以说它类似于 比较引用指向的对象的地址。)

而在的情况下,等于比较两个字符串的内容。类的创建者有责任覆盖默认的equals方法,以根据对该对象有意义的对象来比较该类的对象。例如,在String类的情况下,类的创建者已经重写了equals方法来比较字符串的内容。

    String a = "test"; // lets say the object guy has address : 24
    String b = a; // now b points to the same object that is being referenced by a 
    System.out.println(a == b); // this will be true as both point to the same reference
    System.out.println(a.equals(b)); // this will  be true as the contents of both these strings is the same.

    // Now lets consider a new strings having same content "test"
    String c = "test";
    System.out.println(a == c); // this will be false as both point to the different references or memory location
    System.out.println(a.equals(c)); // this will be true as the contents of both these strings is the same.