Question

In: Computer Science

Often we would like to make the user think we like him/her so we use a...

Often we would like to make the user think we like him/her so we use a friendly prompt to get them to decide if they want to continue a loop. One can make a sentinel using an integer value outside of the expected input range but a more delightful way is to ask politely if: System.out.println("Do you want to play again?);

In order to do this we will need a scanner and a do-while loop. For the scanner sometimes the compiler seems to malfunction if the same scanner object is used for different data types so I prefer to use 2 scanner object- one for numbers and one for strings, rather than one object with a wrapper/parser.

So set up: Scanner again = new Scanner(system.in);

String another="";

do {

//whatever stuff you want to do like an inner loop to play a game like Hi-Lo

System.out.println("Do you want to play again?);

another=again.nextLine();

}

while(another.equalsIgnoreCase("yes") || another.equalsIgnoreCase("y"));

This is Java Please

Solutions

Expert Solution

import java.util.Scanner;

public class Prompt {
        public static void main(String[] args) {
                Scanner again = new Scanner(System.in);
                Scanner sc = new Scanner(System.in);
                String another = "";

                do {
                        System.out.println("Heads-1 or Tails - 0?");
                        int ch = sc.nextInt();
                        int x = (int) (System.currentTimeMillis() % 2);
                        if (x == ch) {
                                System.out.println("You won toss");
                        } else {
                                System.out.println("You lost toss");
                        }
                        System.out.println("Do you want to play again?");

                        another = again.nextLine();

                }

                while (another.equalsIgnoreCase("yes") || another.equalsIgnoreCase("y"));
        }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

1) A mother would like to pick up Tylenol OTC for her child due to him...
1) A mother would like to pick up Tylenol OTC for her child due to him having a low-grade fever. The child is 3-year old boy and weighs 30 pounds. She picks up a Tylenol oral solution with a concentration of 160 mg/5 mL. Using appropriate drug references, what is an appropriate dose and dosing frequency to advise the mother to give her child? (Provide answer in milliliters and round to two decimal places). 2) A patient is prescribed gabapentin...
Shopping Bag: Assume the user has a bunch of purchases they would like to make. They...
Shopping Bag: Assume the user has a bunch of purchases they would like to make. They enter the price of each item, and the quantity and they want to add it to their cart. They tell us if they have more items to purchase, and if they do, we repeat getting the data and add the cost to the shopping bag. When they're done, we display the total. We will not account for Tax (yet) We will create a textual...
We often think of important parts of the physical world like our own bodies, in concrete...
We often think of important parts of the physical world like our own bodies, in concrete terms. Explain why the body can be seen as a cultural construct as well. Can you think of anything and examples in the past or present
We often think about two-dimensional motion in terms of a projectile, like someone throwing a ball...
We often think about two-dimensional motion in terms of a projectile, like someone throwing a ball up in the air. Consider, instead, the surface of an air-hockey table, where the puck travels horizontally from one end of the table to the other. Imagine you’re standing at one end of the table and answer the following questions in your initial post to the discussion. 1. Describe the shape of the puck’s path, starting from the end of the table where you’re...
We often think about two-dimensional motion in terms of a projectile, like someone throwing a ball...
We often think about two-dimensional motion in terms of a projectile, like someone throwing a ball up in the air. Consider, instead, the surface of an air-hockey table, where the puck travels horizontally from one end of the table to the other. Imagine you’re standing at one end of the table and answer the following questions in your initial post to the discussion. Describe the shape of the puck’s path, starting from the end of the table where you’re standing,...
Are we taking more vitamin supplements than our parents? If so or not I would like...
Are we taking more vitamin supplements than our parents? If so or not I would like you to please analyze how lifestyle changes between our parents and ours that have influenced this behavior of taking more or less vitamin supplements.
We would like to analyze expenditures on research and development and use regression analysis. We assume...
We would like to analyze expenditures on research and development and use regression analysis. We assume that total expenditures would be closely related to the income , GDP, investments and inflation rate. Please describe results and quality of regression model. Formulate inferences about the regression model parameters. Test usefulness of the model. Are parameter statistically significant? Please set up correct hypothesis and formulate your conclusions. SUMMARY OUTPUT Regression Statistics Multiple R 0,9499 R Square 0,9023 Adjusted R Square 0,8878 Standard...
Do you think a fixed exchange rate would make sense for a large economy like ours?
Do you think a fixed exchange rate would make sense for a large economy like ours?
Dyna tell Ed that she will pay him $1,000 to set fire to her store so...
Dyna tell Ed that she will pay him $1,000 to set fire to her store so she can collect under a fire insurance policy. Ed sets fire to the store and Dyna refuses to pay. Ed sues Dyna. Does Ed win. Please explain why or why not.  
Why would a dissonance theorist joke that no decisions feel like good ones, so we have...
Why would a dissonance theorist joke that no decisions feel like good ones, so we have to convince ourselves that they are?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT