如何使用if重复一个字符串..我想让程序如果他们说是的话

时间:2015-02-22 09:29:28

标签: string if-statement

请看底部,你可以看到我的错误=)

 import java.util.Scanner;
 public class BlueJPeriodicalExam {
 static Scanner key = new Scanner(System.in);
 public static void main(String[] args) {

    System.out.println("Greetings, user!");
    System.out.println("Please type your name:");
    String a = key.nextLine();
    System.out.println("");

    System.out.println("Please choose from the following: things, names, or places");
    String choice= key.next();
    System.out.println("");
    if(choice.equals("things")) System.out.println("You chose things.");
    if(choice.equals("names"))  System.out.println("You chose names.");
    if(choice.equals("places")) System.out.println("You chose places.");
    System.out.println("");

    System.out.println("Please type any letter from A-Z.");
    String b = key.next();
    System.out.println("");
    if(b.equals("a"))
    {
        if(choice.equals("things")) System.out.println("Find an Arrow.");
        if(choice.equals("names"))  System.out.println("You voted for Albert Einstein.");
        if(choice.equals("places")) System.out.println("Please visit America.");
        System.out.println("");
    }
    if(b.equals("A"))
    {
        if(choice.equals("things")) System.out.println("Find an Arrow.");
        if(choice.equals("names"))  System.out.println("You voted for Albert Einstein.");
        if(choice.equals("places")) System.out.println("Please visit America.");
        System.out.println("");
    }
    if(b.equals("b"))
    {
        if(choice.equals("things")) System.out.println("Find a Bag.");
        if(choice.equals("names"))  System.out.println("You voted for Barry Allen.");
        if(choice.equals("places")) System.out.println("Please visit Britain.");
        System.out.println("");
    }
    if(b.equals("B"))
    {
        if(choice.equals("things")) System.out.println("Find a Bag.");
        if(choice.equals("names"))  System.out.println("You voted for Barry Allen.");
        if(choice.equals("places")) System.out.println("Please visit Britain.");
        System.out.println("");
    }
    if(b.equals("c"))
    {
        if(choice.equals("things")) System.out.println("Find a Cabinet.");
        if(choice.equals("names"))  System.out.println("You voted for Caitlyn Snow.");
        if(choice.equals("places")) System.out.println("Please visit Canada.");
        System.out.println("");
    }
    if(b.equals("C"))
    {
        if(choice.equals("things")) System.out.println("Find a Cabinet.");
        if(choice.equals("names"))  System.out.println("You voted for Caitlyn Snow.");
        if(choice.equals("places")) System.out.println("Please visit Canada.");
        System.out.println("");
    }
    if(b.equals("d"))
    {
        if(choice.equals("things")) System.out.println("Find a Door.");
        if(choice.equals("names"))  System.out.println("You voted for Dexter Santos.");
        if(choice.equals("places")) System.out.println("Please visit Denmark.");
        System.out.println("");
    }
    if(b.equals("D"))
    {
        if(choice.equals("things")) System.out.println("Find a Door.");
        if(choice.equals("names"))  System.out.println("You voted for Dexter Santos.");
        if(choice.equals("places")) System.out.println("Please visit Denmark.");
        System.out.println("");
    }
    if(b.equals("e"))
    {
        if(choice.equals("things")) System.out.println("Find an Eraser.");
        if(choice.equals("names"))  System.out.println("You voted for Eddie Thawne.");
        if(choice.equals("places")) System.out.println("Please visit England.");
        System.out.println("");
    }
    if(b.equals("E"))
    {
        if(choice.equals("things")) System.out.println("Find an Eraser.");
        if(choice.equals("names"))  System.out.println("You voted for Eddie Thawne.");
        if(choice.equals("places")) System.out.println("Please visit England.");
        System.out.println("");
    }
    if(b.equals("f"))
    {
        if(choice.equals("things")) System.out.println("Find a Fence.");
        if(choice.equals("names"))  System.out.println("You voted for Felicity Smoak.");
        if(choice.equals("places")) System.out.println("Please visit Finland.");
        System.out.println("");
    }
    if(b.equals("F"))
    {
        if(choice.equals("things")) System.out.println("Find a Fence.");
        if(choice.equals("names"))  System.out.println("You voted for Felicity Smoak.");
        if(choice.equals("places")) System.out.println("Please visit Finland.");
        System.out.println("");
    }
    if(b.equals("g"))
    {
       if(choice.equals("things")) System.out.println("Find a Grinder.");
       if(choice.equals("names"))  System.out.println("You voted for Galileo Galilei.");
       if(choice.equals("places")) System.out.println("Please visit Greenland.");
       System.out.println("");
    }
    if(b.equals("G"))
    {
       if(choice.equals("things")) System.out.println("Find a Grinder.");
       if(choice.equals("names"))  System.out.println("You voted for Galileo Galilei.");
       if(choice.equals("places")) System.out.println("Please visit Greenland.");
       System.out.println("");
    }
    if(b.equals("h"))
    {
       if(choice.equals("things")) System.out.println("Find a Hairbrush.");
       if(choice.equals("names"))  System.out.println("You voted for Harrison Wells.");
       if(choice.equals("places")) System.out.println("Please visit Hong Kong.");
       System.out.println("");
    }
    if(b.equals("H"))
    {
       if(choice.equals("things")) System.out.println("Find a Hairbrush.");
       if(choice.equals("names"))  System.out.println("You voted for Harrison Wells.");
       if(choice.equals("places")) System.out.println("Please visit Hong Kong.");
       System.out.println("");
    }
    if(b.equals("i"))
    {
       if(choice.equals("things")) System.out.println("Find an Instrument.");
       if(choice.equals("names"))  System.out.println("You voted for Isaac Newton.");
       if(choice.equals("places")) System.out.println("Please visit Ireland.");
       System.out.println("");
    }
    if(b.equals("I"))
    {
       if(choice.equals("things")) System.out.println("Find an Instrument.");
       if(choice.equals("names"))  System.out.println("You voted for Isaac Newton.");
       if(choice.equals("places")) System.out.println("Please visit Ireland.");
       System.out.println("");
    }
    if(b.equals("j"))
    {
      if(choice.equals("things")) System.out.println("Find a Jar.");
      if(choice.equals("names"))  System.out.println("You voted for James Chadwick.");
      if(choice.equals("places")) System.out.println("Please visit Japan.");
      System.out.println("");
    }
    if(b.equals("J"))
    {
      if(choice.equals("things")) System.out.println("Find a Jar.");
      if(choice.equals("names"))  System.out.println("You voted for James Chadwick.");
      if(choice.equals("places")) System.out.println("Please visit Japan.");
      System.out.println("");
    }
    if(b.equals("k"))
    {
      if(choice.equals("things")) System.out.println("Find a Kite.");
      if(choice.equals("names"))  System.out.println("You voted for Konrad Lorenz.");
      if(choice.equals("places")) System.out.println("Please visit Korea.");
      System.out.println("");
    }
    if(b.equals("K"))
    {
      if(choice.equals("things")) System.out.println("Find a Kite.");
      if(choice.equals("names"))  System.out.println("You voted for Konrad Lorenz.");
      if(choice.equals("places")) System.out.println("Please visit Korea.");
      System.out.println("");
    }
    if(b.equals("l"))
    {
      if(choice.equals("things")) System.out.println("Find a Log.");
      if(choice.equals("names"))  System.out.println("You voted for Luise Andrei Pilar.");
      if(choice.equals("places")) System.out.println("Please visit Libya.");
      System.out.println("");
    }
    if(b.equals("L"))
    {
      if(choice.equals("things")) System.out.println("Find a Log.");
      if(choice.equals("names"))  System.out.println("You voted for Luise Andrei Pilar.");
      if(choice.equals("places")) System.out.println("Please visit Libya.");
      System.out.println("");
    }
    if(b.equals("m"))
    {
      if(choice.equals("things")) System.out.println("Find a Map.");
      if(choice.equals("names"))  System.out.println("You voted for Michael John D Cunanan.");
      if(choice.equals("places")) System.out.println("Please visit Manchester.");
      System.out.println("");
    }
    if(b.equals("M"))
    {
      if(choice.equals("things")) System.out.println("Find a Map.");
      if(choice.equals("names"))  System.out.println("You voted for Michael John D Cunanan.");
      if(choice.equals("places")) System.out.println("Please visit Manchester.");
      System.out.println("");
    }
    if(b.equals("n"))
    {
      if(choice.equals("things")) System.out.println("Find a Notebook.");
      if(choice.equals("names"))  System.out.println("You voted for Nariszah Montecillo.");
      if(choice.equals("places")) System.out.println("Please visit Norway.");
      System.out.println("");
    }
    if(b.equals("N"))
    {
      if(choice.equals("things")) System.out.println("Find a Notebook.");
      if(choice.equals("names"))  System.out.println("You voted for Nariszah Montecillo.");
      if(choice.equals("places")) System.out.println("Please visit Norway.");
      System.out.println("");
    }
    if(b.equals("o"))
    {
      if(choice.equals("things")) System.out.println("Find an Oven.");
      if(choice.equals("names"))  System.out.println("You voted for Oliver Quinn.");
      if(choice.equals("places")) System.out.println("Please visit Oman.");
      System.out.println("");
    }
    if(b.equals("O"))
    {
      if(choice.equals("things")) System.out.println("Find an Oven.");
      if(choice.equals("names"))  System.out.println("You voted for Oliver Quinn.");
      if(choice.equals("places")) System.out.println("Please visit Oman.");
      System.out.println("");
    }
    if(b.equals("p"))
    {
      if(choice.equals("things")) System.out.println("Find a Pan.");
      if(choice.equals("names"))  System.out.println("You voted for Philip Phillips.");
      if(choice.equals("places")) System.out.println("Please visit Philippines.");
      System.out.println("");
    }
    if(b.equals("P"))
    {
      if(choice.equals("things")) System.out.println("Find a Pan.");
      if(choice.equals("names"))  System.out.println("You voted for Philip Phillips.");
      if(choice.equals("places")) System.out.println("Please visit Philippines.");
      System.out.println("");
    }
    if(b.equals("q"))
    {
      if(choice.equals("things")) System.out.println("Find a Quantum Splicer.");
      if(choice.equals("names"))  System.out.println("You voted for Quilla Ford.");
      if(choice.equals("places")) System.out.println("Please visit Qatar.");
      System.out.println("");
    }
    if(b.equals("Q"))
    {
      if(choice.equals("things")) System.out.println("Find a Quantum Splicer.");
      if(choice.equals("names"))  System.out.println("You voted for Quilla Ford.");
      if(choice.equals("places")) System.out.println("Please visit Qatar.");
      System.out.println("");
    }
    if(b.equals("r"))
    {
      if(choice.equals("things")) System.out.println("Find a Rug.");
      if(choice.equals("names"))  System.out.println("You voted for Robbie Amell.");
      if(choice.equals("places")) System.out.println("Please visit Russia.");
      System.out.println("");
    }
    if(b.equals("R"))
    {
      if(choice.equals("things")) System.out.println("Find a Rug.");
      if(choice.equals("names"))  System.out.println("You voted for Robbie Amell.");
      if(choice.equals("places")) System.out.println("Please visit Russia.");
      System.out.println("");
    }
    if(b.equals("s"))
    {
      if(choice.equals("things")) System.out.println("Find a Sharpener.");
      if(choice.equals("names"))  System.out.println("You voted for Sona Raymond.");
      if(choice.equals("places")) System.out.println("Please visit Singapore.");
      System.out.println("");
    }
    if(b.equals("S"))
    {
      if(choice.equals("things")) System.out.println("Find a Sharpener.");
      if(choice.equals("names"))  System.out.println("You voted for Sona Raymond.");
      if(choice.equals("places")) System.out.println("Please visit Singapore.");
      System.out.println("");
    }
    if(b.equals("t"))
    {
      if(choice.equals("things")) System.out.println("Find a Telephone.");
      if(choice.equals("names"))  System.out.println("You voted for Tony Woodward.");
      if(choice.equals("places")) System.out.println("Please visit Thailand.");
      System.out.println("");
    }
    if(b.equals("T"))
    {
      if(choice.equals("things")) System.out.println("Find a Telephone.");
      if(choice.equals("names"))  System.out.println("You voted for Tony Woodward.");
      if(choice.equals("places")) System.out.println("Please visit Thailand.");
      System.out.println("");
    }
    if(b.equals("u"))
    {
      if(choice.equals("things")) System.out.println("Find an Umbrella.");
      if(choice.equals("names"))  System.out.println("You voted for Uriel Septum.");
      if(choice.equals("places")) System.out.println("Please visit Ukraine.");
      System.out.println("");
    }
    if(b.equals("U"))
    {
      if(choice.equals("things")) System.out.println("Find an Umbrella.");
      if(choice.equals("names"))  System.out.println("You voted for Uriel Septum.");
      if(choice.equals("places")) System.out.println("Please visit Ukraine.");
      System.out.println("");
    }
    if(b.equals("v"))
    {
      if(choice.equals("things")) System.out.println("Find a Violin.");
      if(choice.equals("names"))  System.out.println("You voted for Vigay Kumar.");
      if(choice.equals("places")) System.out.println("Please visit Vietnam.");
      System.out.println("");
    }
    if(b.equals("V"))
    {
      if(choice.equals("things")) System.out.println("Find a Violin.");
      if(choice.equals("names"))  System.out.println("You voted for Vigay Kumar.");
      if(choice.equals("places")) System.out.println("Please visit Vietnam.");
      System.out.println("");
    }
    if(b.equals("w"))
    {
      if(choice.equals("things")) System.out.println("Find a Wood.");
      if(choice.equals("names"))  System.out.println("You voted for William Shakespeare.");
      if(choice.equals("places")) System.out.println("Please visit Washington D.C.");
      System.out.println("");
    }
    if(b.equals("W"))
    {
      if(choice.equals("things")) System.out.println("Find a Wood.");
      if(choice.equals("names"))  System.out.println("You voted for William Shakespeare.");
      if(choice.equals("places")) System.out.println("Please visit Washington D.C.");
      System.out.println("");
    }
    if(b.equals("x"))
    {
      if(choice.equals("things")) System.out.println("Find a Xylophone.");
      if(choice.equals("names"))  System.out.println("You voted for Xin Zhao.");
      if(choice.equals("places")) System.out.println("Please visit Xiao Pei Street.");
      System.out.println("");
    }
    if(b.equals("X"))
    {
      if(choice.equals("things")) System.out.println("Find a Xylophone.");
      if(choice.equals("names"))  System.out.println("You voted for Xin Zhao.");
      if(choice.equals("places")) System.out.println("Please visit Xiao Pei Street.");
      System.out.println("");
    }
    if(b.equals("y"))
    {
      if(choice.equals("things")) System.out.println("Find a Yoyo.");
      if(choice.equals("names"))  System.out.println("You voted for Yamagato.");
      if(choice.equals("places")) System.out.println("Please visit Yemen.");
      System.out.println("");
    }
    if(b.equals("Y"))
    {
      if(choice.equals("things")) System.out.println("Find a Yoyo.");
      if(choice.equals("names"))  System.out.println("You voted for Yamagato.");
      if(choice.equals("places")) System.out.println("Please visit Yemen.");
      System.out.println("");
    }
    if(b.equals("z"))
    {
      if(choice.equals("things")) System.out.println("Find a Zipper.");
      if(choice.equals("names"))  System.out.println("You voted for Zayn Malik.");
      if(choice.equals("places")) System.out.println("Please visit Zimbabwe.");
      System.out.println("");
    }
    if(b.equals("Z"))
    {
      if(choice.equals("things")) System.out.println("Find a Zipper.");
      if(choice.equals("names"))  System.out.println("You voted for Zayn Malik.");
      if(choice.equals("places")) System.out.println("Please visit Zimbabwe.");
      System.out.println("");
    }

在这部分我需要你的帮助。

在这部分我需要你的帮助。

在这部分我需要你的帮助。

我想用(问候,用户)

重复上面的字符串
    System.out.println("Would you still like to continue? Type: yes or no");
    String c = key.next();
    System.out.println("");
    if(c.equals("no")) 
    {
        System.out.println("Thanks for cooperating with us " + a + ". You may now exit the program.");
    }
    if(c.equals("yes")) 
    {
                       //// I NEED YOU HELP IN HERE =)
    }
    System.out.println("");
   }
}

感谢您的帮助!希望你能帮助我这个

1 个答案:

答案 0 :(得分:0)

所以你希望程序一遍又一遍地问,直到你输入“no”?

String c = "";
while (!c.equals("no")){
    System.out.println("Would you still like to continue? Type: yes or no");
    String c = key.next();
    System.out.println("");
}
System.out.println("Thanks for cooperating with us " + a + ". You may now exit the program.");
System.out.println("");

顺便说一句,在字符串上使用maps和.toLowerCase()。这将使您的代码更短:

import java.util.Scanner;
    public class BlueJPeriodicalExam {
    static Scanner key = new Scanner(System.in);
    public static void main(String[] args) {
        System.out.println("Greetings, user!\nPlease type your name:");
        String name = key.nextLine();
        System.out.println("\nPlease choose from the following: things, names, or places");
        String choice= key.next();

        int choiceInt = 0;

        if(choice.equals("things")) choiceInt = 0;
        if(choice.equals("names")) choiceInt = 1;
        if(choice.equals("places")) choiceInt = 2;

        System.out.println("You chose"+choice+".\n");


        Map<string, List<string>> map = new HashMap<string, List<string>>();

        List<string> mylist = ["Find an Arrow.","You voted for Albert Einstein.","Please visit America."],
        map.Add("a", mylist);
        mylist = ["Find a Bag.","You voted for Barry Allen.","Please visit Britain."];
        map.Add("b", mylist);
        mylist = ["Find a Cabinet.","You voted for Caitlyn Snow.","Please visit Canada."];
        map.Add("c", mylist);
        mylist = ["Find a Door.","You voted for Dexter Santos.","Please visit Denmark."];
        map.Add("d", mylist);
        mylist = ["Find an Eraser.","You voted for Eddie Thawne.","Please visit England."];
        map.Add("e", mylist);
        mylist = ["Find a Fence.","You voted for Felicity Smoak.","Please visit Finland."];
        map.Add("f", mylist);

        ...
        ....
        .....


        System.out.println("Please type any letter from A-Z.");
        String b = key.next();

        System.out.println(map[b.toLowerCase()][choiceInt]);
        System.out.println("");


        String c = "";
        while (!c.equals("no")){
            System.out.println("Would you still like to continue? Type: yes or no");
            String c = key.next();
            System.out.println("");
        }
        System.out.println("Thanks for cooperating with us, " + name + ". You may now exit the program.");
        System.out.println("");
    }
}