Ask coding questions

← Back to all posts
Need help with writing booleans in java
MadisonLouton (0)

so I'm writing code this is the prompt:

Write a short quiz program that asks three true/false questions and stores the user's answers as booleans. At the end of the program, it should print the user's answers with the correct answers.

Note: you should use the same quiz questions and correct answers as the sample run below.

Sample run:

Java is a programming language, true or false?
true
Only numbers can be stored by variables in Java, true or false?
true
There are only 2 possible values that can be held by a boolean variable, true or false?
false
Question 1 - Your answer: true. Correct answer: true
Question 2 - Your answer: true. Correct answer: false
Question 3 - Your answer: false. Correct answer: true

the only code I have is listed below and i'm not sure if is right either. I'm just confused please help. Thanks.

/ Lesson 3 Coding Activity Question 3 /

import java.util.Scanner;

public class U1_L3_Activity_Three{
public static void main(String[] args){

Scanner scan = new Scanner(System.in);
String t;
String f;


System.out.println("Java is a programming language, true or false?");
t = scan.nextLine();
System.out.println("Only numbers can be stored by variables in Java, true or false?");
f = scan.nextLine();

}

}

Answered by SixBeeps (5346) [earned 5 cycles]
View Answer
Comments
hotnewtop
SixBeeps (5346)

Instead of nextLine(), use nextBoolean(). That and make t and f into boolean variables.

PattanAhmed (1412)

@MadisonLouton Hi,
On line 5 of your code,
Instead of this:-

t = scan.nextLine();

Turn into this:-

t = scan.nextBoolean();

This will automatically make t and f into boolean variables in the later use.

That's it.
Thanks!

Hope this helps

JackyApril (2)

Why do you need to read essay service reviews? Well, we need to read what others think about the writer and their writing. This will give us insight into the writer's personality. This will answer questions about whether a writer can write a well-written resume that your teacher will love. It is for this reason that I always read WritngJudge - https://www.writingjudge.com/ reviews and choose the best writing services.

PattanAhmed (1412)

@MadisonLouton Welcome!
Happy to help you.

Please mark my answer if my post helps
Thanks!