简体   繁体   中英

Write a Java program that asks the user if he/she likes Java programming

Homework assignment:

Write a Java program that asks the user if he/she likes Java programming.

If the user answers yes, then it displays a congratulating message (use your imagination),and terminates.

Now, if the user answers no, then the fun starts.

– The program should ask Are you sure you don't like Java programming?.

∗ If the user answers no, then it should display Hope you'll get to like it soon...

∗ Otherwise, if the user answers yes, it should ask Are you really sure you don't like Java programming?, and repeat.

· If the user keeps saying yes, then the program keeps asking

Are you really really sure you don't like Java programming?,

then Are you really really really sure you don't like Java programming?.

And so on, every time printing one more really.

· This should stop when it reaches the point when it prints really 5 times.

At that point, if the user still answers yes, it should print Too bad.., and terminate.

I have coded the basic switch case for this however the core answer which includes the repetition of really, should be a for loop or a nested for loop. These loops really confuse me so I need some help with it.

package lab7;

import java.util.Scanner;

public class problem7 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Do you like java programming? 'yes' / 'no'");
        String input = scan.next();

        switch (input) {
            case "yes":
            case "Yes":
                System.out.println("Congratulations, you are an advanced intellectual");
                break;
            case "no":
                System.out.println("Are you sure you don't like java programming?");
                String input2 = scan.next();
                switch (input2) {
                    case "no":
                    case "No":
                        System.out.println("Hope you’ll get to like it soon...");
                        break;
                    case "yes":
                    case "Yes":
                        System.out.println("Are you really sure you don’t like Java programming?");
                }
        }
    }
}

SAMPLE RUN 1

Do you like Java programming? yes or no: no

Are you sure you don 't like Java programming? yes or no: yes

Are you really sure you don 't like Java programming? yes or no: yes

Are you really really sure you don 't like Java programming? yes or no: no

Hope you ' ll get to like it soon..

SAMPLE RUN 2

Do you like Java programming? yes or no: no

Are you sure you don 't like Java programming? yes or no: yes

Are you really sure you don 't like Java programming? yes or no: yes

Are you really really sure you don 't like Java programming? yes or no: yes

Are you really really really sure you don 't like Java programming? yes or no: yes

Are you really really really really sure you don 't like Java programming? yes or no: yes

Are you really really really really really sure you don 't like Java programming? yes or no: yes

Too bad..

The basic flow of the looping really part will be like

for (int i = 1; i < 5; i++) {
    System.out.println("get user input");

    // test to see if Yes or No (omitted)

    String out = "";
    for (int inner = 0; inner  < i; inner++) {
        out = out + "really, ";
    }
    System.out.println(out);  // you will want to build the proper String
}

When user want to give his/her name at run time and takes back name as a output in return.

import java.util.Scanner;

public class WriteYourName {

public static void main(String[] args) {

    Scanner stdin = new Scanner( System.in );

    String yourName;      // Entered by the user.
    String yourUpperCaseName;  // Change your name to upper case letters.
    
    System.out.print("Please enter your good name: ");
    yourName = stdin.nextLine();
    
    yourUpperCaseName = yourName.toUpperCase();
    
    System.out.println("Hiii, " + yourUpperCaseName + ", How are you buddy?");

}  

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM