java easy ifs worng output [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am new to java, and currently Im playing around with simple if statments. I have two questions.
x
First one: if answer yes, the last statment "You need to answer yes or no" is printed anyway. I only want it to print if the answer is something other than yes or no.
Second question. If I answer "maybe" (or something that is not yes/no the system print "You have to answer yes or no" and quits. How can a jump back to the first line and ask "are you doing good) once more?
import java.util.Scanner;
public class ovingto {
public static void main (String [] args){
Scanner keyboard = new Scanner(System.in);
System.out.printf("Are you doing good?");
String ord = keyboard.nextLine();
if (ord.equalsIgnoreCase("yes")) {
System.out.println ("nice to hear!");
}
if (ord.equalsIgnoreCase("no")) {
System.out.println ("that makes me sad!");
} else {
System.out.println ("you need to answer yes or no");
}
}
}

The answer to both the questions ..
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
while(true) {
System.out.printf("Are you doing good?");
String ord = keyboard.nextLine();
if (ord.equalsIgnoreCase("yes")) {
System.out.println ("nice to hear!");
break;
}
else if (ord.equalsIgnoreCase("no")) {
System.out.println ("that makes me sad!");
break;
}
else {
System.out.println("you need to answer yes or no");
}
}
}
to redirect use the above code.. also there are many other ways to do it

You need to chain your if/else statements to achieve the effect you want.
Change the code to look like this:
import java.util.Scanner;
public class ovingto {
public static void main (String [] args){
Scanner keyboard = new Scanner(System.in);
System.out.printf("Are you doing good?");
String ord = keyboard.nextLine();
if (ord.equalsIgnoreCase("yes")) {
System.out.println ("nice to hear!");
}
else if (ord.equalsIgnoreCase("no")) {
System.out.println ("that makes me sad!");
}
else {
System.out.println ("you need to answer yes or no");
}
}
}

You are missing an else.
try,
if (ord.equalsIgnoreCase("yes")) {
System.out.println ("nice to hear!");
}
else if (ord.equalsIgnoreCase("no")) {
System.out.println ("that makes me sad!");
}
else {
System.out.println ("you need to answer yes or no");
}
For the 2nd question the best choice is a do-While loop.

Related

How to make the program end after 3 questions in java?

I made a program that answers individual questions and I would like it to end after asking 3 questions in any order. I only did that when I write Bye, the program will end and I don't know how to turn it into these 3 questions. I would like the answer to this question to be related to this code, please
import java.util.Scanner;
class Bot {
public static void main(String[] args) {
System.out.println("QUESTIONS: How old are you? | What is your name? | What did you eat for breakfast?");
System.out.println("Ask a question?");
Scanner pytania = new Scanner(System.in);
String input;
boolean running = true;
while (running) {
System.out.println(" ");
input = pytania.nextLine();
if (input.equals("How old are you?")) {
System.out.println("I am 125 years old");
} else if (input.equals("Bye")) {
System.out.println("Bye!");
running = false;
} else if (input.equals("What is your name?")) {
System.out.println("My name is Christopher");
} else if (input.equals("What did you eat for breakfast?")) {
System.out.println("I had scrambled eggs for breakfast");
} else {
System.out.println("Sorry, I don't understand the question!");
}
}
}
}
Change running from boolean to int. Call it runningCount.
Establish your while loop to only run while runningCount < 3.
In each question, increment runningCount.
The application will stop after you ask three questions as you require.

How do I go back to the menu after taking several questions?

I am currently creating a program where the user enters a specific set of questions. And the program must go back to the menu after completely answering all questions. How should I do it?
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("""
\n \nAre you ready to take the quiz?
Enter "Y" to proceed or "N" to exit the program:""");
String TakeQuiz = input.nextLine();
if (TakeQuiz.equalsIgnoreCase("Y"))
do {
//blocks of code
}
}
}
System.out.println("Do you want to take the quiz again?");
String RetakeQuiz = input.nextLine();
while (RetakeQuiz.equalsIgnoreCase("Y")) ;
else {
System.out.println("We hope to see you again soon!");
System.exit(0);
}
}
}
There are many ways to achieve what you want, I would not clutter the main method and break the code to another function and loop there.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
for(;;)
takeQuiz();
}
public static void takeQuiz(){
Scanner input = new Scanner(System.in);
System.out.print("\n \nAre you ready to take the quiz?" +
"Enter \"Y\" to proceed or \"N\" to exit the program:");
String takeQuiz = input.nextLine();
if (takeQuiz.equalsIgnoreCase("Y")) {
System.out.println("Running code...");
System.out.println("Question 1");
System.out.println("Question 2");
System.out.println("Question 3");
}
// retake
if (takeQuiz.equalsIgnoreCase("R")){
takeQuiz();
}
if (takeQuiz.equalsIgnoreCase("N")){
System.out.println("We hope to see you again soon!");
System.exit(0);
}
}
}
Notice the escape character for quotes \" and the + for multiline Strings
Java 15 and beyond allows triple quotes as Java Text Blocks
so your String message should be valid
The basic structure is something like this:
boolean continueWithQuiz = true;
while (continueWithQuiz) {
// Put the code here for handling the quiz
...
// Should we keep going?
System.out.println("Do you want to take the quiz again?");
String retakeQuiz = input.nextLine();
continueWithQuiz = retakeQuiz == "Y";
}
One more comment. Please follow Java naming standards. Class names begin with an upper case letter. Constants should be ALL_CAPS. Everything else is in lower case.

Java: if statement with not equals breaking the program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I can't figure out why my code isn't working.
It appears to be breaking around the if not equal to yes or no area.
Here's my code:
public static void main(String[] args) {
// TODO code application logic here
Scanner user_input = new Scanner(System.in);
String name;
System.out.println("Hello, what is your name?");
name = user_input.next();
System.out.println("");
String name_answer;
System.out.println("Your name is " + name + ". Is this correct? (yes/no)");
name_answer = user_input.next();
System.out.println("");
if (!name_answer.equals("yes" + "no")) {
System.out.println("Answer not valid. Please input again.");
name_answer = user_input.next();
while (!name_answer.equals("yes" + "no")) {
System.out.println("Answer not valid. Please input again.");
name_answer = user_input.next(); } }
if (name_answer.equals("yes")) {
System.out.println("Thank you, " + name + ". Please proceed to the next question.");
} else if (name_answer.equals("no")) {
System.out.println("Please reinput your name correctly.");
while (name_answer.equals("no")) {
String name_again;
System.out.println("");
System.out.println("What is your correct name?");
name_again = user_input.next();
System.out.println("");
System.out.println("Your name is " + name_again + ". Is this correct? (yes/no)");
name_answer = user_input.next(); }
If i comment out the not-equals block of code (displayed below), the program works. However, with the block of code in, the program breaks.
if (!name_answer.equals("yes" + "no")) {
System.out.println("Answer not valid. Please input again.");
name_answer = user_input.next();
while (!name_answer.equals("yes" + "no")) {
System.out.println("Answer not valid. Please input again.");
name_answer = user_input.next(); } }
My goal is to have any answer not equal to "yes" or "no" be reinputted while a "yes" or "no" brings the program to another step. Thanks for the help.
The technical problem with your code is that you're using concatenation instead of logical operators. "yes" + "no" evaluates to "yesno", which will probably never match your input string.
More fundamentally, the problem is that you're trying to bundle two boolean evaluations into one. Logically, you want to proceed if the answer is not "yes" and the answer is not "no". In Java syntax:
if (!name_answer.equals("yes") && !name_answer.equals("no")) {
If you want to test multiple values at a time, you can use this shortcut:
if (!Arrays.asList("yes", "no", "foo", "bar").contains(name_answer)) {

Two if statements with different variables

Ok so im trying to make it so if the person does not want to enter their age the program will print out a different answer. However, when i do this it gives me an error for the string. I used // to make it so the int answer wasnt being played and it worked then. How exactly would I make it so they both work for the same question? I searched for an answer but I couldnt seem to find it so if there is a link for this please link me. Thanks!
System.out.println("So how old are you?");
TimeUnit.SECONDS.sleep(2);
System.out.println("If you dont want to answer you dont have to. ");
Scanner scan4 = new Scanner (System.in);
String user_imput_string1 = scan.nextLine();
if (user_imput_string1.equals("I dont know")) {
System.out.println("Ah thats cool. You look great regardless of your age anyway");
} else {
System.out.println("Ah thats cool. You look great regardless of your age anyway");
}
You would need to convert the String into an int in order to compare the value to 30. However, looking at your code, you seem to have two different variables already, user_imput_string1 and user_imput_int, the latter of which is still a String.
Here is the sample code you could use in order to correctly convert from a String to an int:
int result = Integer.parseInt(user_imput_int);
if (result > 30){
// do whatever
}
Also, as a side note, you are spelling input wrong.
You can do this by catching Exception
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("So how old are you? ");
String age = scanner.next(); //Read string by default
try{
int actualAge = Integer.parseInt(age);
//do your stuff with age
}catch(Exception e){ //Raises NumberFormatException if it's not a number
//e.printStackTrace();
System.out.println("Ah thats cool. You look great regardless of your age anyway");
}
}
The code is below,I hope it can help you.
System.out.println("So how old are you?");
TimeUnit.SECONDS.sleep(2);
System.out.println("If you dont want to answer you dont have to. ");
Scanner scan = new Scanner(System.in);
String user_imput_int = scan.next();
if ("I dont know".equals(user_imput_int)) {
System.out.println("Ah thats cool. You look great regardless of your age anyway");
} else {
try {
int age = Integer.parseInt(user_imput_int);
if(age > 30)
{
System.out.println("Oh wow you look so good");
}
else {
System.out.println("Oh thats ok. You look great regardless");
}
} catch (Exception e) {
System.out.println("your input is either 'I dont know' or int number");
}
}

how to create a counter in a dr Java program

/*This is a quiz program that will ask the user 10 questions. the user will answer
* these questions and will be scored out of 10.*/
class Quiz {
public static void main(String args[]) {
// Instructions
System.out.println("instructions");
System.out.println(" ");
System.out
.println("1. You wll be asked ten questions through out the quiz.");
System.out
.println("2. The first question will appear, you will have to answer that question for the next question to appear.");
System.out
.println("3. When you answer the last question you will be told your score.");
System.out.println(" ");
System.out.println("welcome to the basketball quiz.");
// question 1
System.out.println(" ");
System.out.println("Question 1. ");
System.out.println("How tall is a basketball hoop? ");
System.out.println("Type in Answer here:");
String Question1 = In.getString();
if (Question1.equalsIgnoreCase("10 Feet")) {
System.out.println("Correct!");
} else {
System.out.println("you got this questions wrong");
}
// question 2
System.out.println(" ");
System.out.println("Question 2. ");
System.out.println("Who invented basketball? ");
System.out.println("Type in Answer here:");
String Question2 = In.getString();
if (Question2.equalsIgnoreCase("James Naismith ")) {
System.out.println("Correct!");
} else {
System.out.println("you got this questions wrong");
}
}
}
This is my program that I am writing. I want to make a counter that will keep score of every question that is answered right and then display it to the user after the questions are finished. I tried using this:
int score=0;
score=score+1;
It doesn't not work for the 2nd question, but works for the 3rd... it gives me an error.
Is there another way I can do this or am I doing something wrong?
It looks like you are on the right track. You need to declare a socre variable at the begiunning of the program.
int score = 0;
Then in each question where you print out "correct" you can increment the score like this:
score++;
At the end of the program after the last question you can print the score.
Maybe you should post the error you got when you tried it.
UPDATE:
The syntax is score++ NOT score=++. That is, take out the = sign.
What you did is correct. Heed the comment on your post; you need semi-colons at the end of your posted solution. Also, per the Java Language Specification, it's best to name your variable with all lower case characters:
int score = 0;
// question code
score += 1;
or
score = score + 1;
or
score++;
You need to place the variable declaration (int score = 0;) outside of any loops (your if/else loops). It would be best to place it at the first line of the main method.
Your problem is possible because you have a whitespace character after the name "James Naismith" in the comparison for their given answer. For it to be evaluated to true the user must answer with the exact string "James Naismith " instead of "James Naismith"
Edit: Nevermind, This should not cause an error but it is something to bring your attention to still because it could affect the result of the program.

Categories

Resources