Can someone help me with my code, I'm trying to create a simple calculator that asks for user input, a value too low will return "no that number is too small try again" a value too large will return "no that number is too large, try again" and the correct answer will output "correct" while ending the program.
It's straightforward but I'm trying to get the hang of looping. It all works fine, but I want it to continuously accept inputs until the correct answer is reached.
i.e
PC: Answer '2 + 2' as an integer
User: 2
PC: "Incorrect, that number is too small. Try again."
User: 6
PC: "Incorrect, that number is too large. Try again."
User: 4
PC: "Correct!"
*ends program*
/*A program whereby the user is asked to solve 2+2,
every false answer reads "incorrect" and must
input another answer until the correct answer is reached
*/
import java.util.Scanner;
public class muckabout {
public static void main(String[] args) {
try(Scanner input = new Scanner(System.in)) {
String question = "Answer '2 + 2' as an integer";
System.out.println(question);
int intIn = input.nextInt() ;
if (intIn < 4) {
System.out.println("That number is too small, try again.");
}
if (intIn > 4) {
System.out.println("That number is too large, try again.");
}
if (intIn == 4) {
System.out.println("Correct, congratulations!");
}
}
}
}
Also how do I format the code for the /g/ board thanks in advance.
>>51660300
consider assisted suicide.
A calculator will never tell you that a number is too small or too big or correct.
Go best up a white nerdy kid to do your homework, Jamal
>>51660300
First off why the fuck are you using a string to input into System.out.println and not directly in it?
Secondly, you want to make a procedure so first have a main method with the void in it like
public static void main(String[] args)
{
for (int i = 1; i < 5; i++)
calc();
}
//calculator shit goes here
Now this is probably incorrect because I need to test it but the general idea is there
>>51660333
Thanks for the advice Kumar, us brown guys need to stick together.
But seriously, it's a looping exercise.
I could just as easily use strings "this bed is too big" or "this bed is too small"
Use a while loop by saying "while (intIn != 4)". Inside it use if and else if statements to say whether it is or isn't right. After the while loop, make another if statement for if it is 4.
>>51660323
this desu senpai
>>51660300
well first you have to make it a dynamic class so make a factory
public class MuckAboutFactory {
public static MuckAbout createMuckAbout(String muckAboutType)
{
//preferably you'd have a switch statement here to instantiate the proper type of muckabout
}
}
I'll leave it from there for you to figure out
Needs more zygohistomorphic prepromorphisms.
>>51660300
I fixed the shit for you now copy it and feel good about yourself
import java.util.Scanner;
public class calctest {
public static void main(String[] args)
{
for (int i =0; i >=0; i++)
{
calc();
}
}
public static void calc()
{
try(Scanner input = new Scanner(System.in))
{
String question = "Answer '2 + 2' as an integer";
System.out.println(question);
int intIn = input.nextInt() ;
if (intIn < 4) {
System.out.println("That number is too small, try again.");
}
if (intIn > 4) {
System.out.println("That number is too large, try again.");
}
if (intIn == 4) {
System.out.println("Correct, congratulations!");
}
}
}
}
>>51660487
This is incorrect btw.
>java
>help
Sure. The door is right there.
>>51660300while (true) {
int intIn = input.nextInt();
if (intIn < 4) {
// Incorrect
}
if (intIn > 4) {
// Incorrect
}
if (intIn == 4) {
// Correct
return;
}
}
>>51660323
Looks like you're gonna need a lot of assistance...
>>51663162
>while(true)
Kill yourself.
>not if, else if, else
>>51661439
Anon, I... I am posting outdoors...
>>51663369x = 5
while True:
x -= 1
print x + 1
if x < 1:
True = False
Are people on /g/ really this bad at coding?
>>51663162Scanner input = new Scanner( System.in );
int intIn = 0;
while( intIn != -1 ) // always give yourself a sentinel value
{
intIn = input.nextInt();
if( intIn == 4 )
break;
else
System.out,println("Incorrect.");
}
>>51661223
Why is it?
>>51660300
Ok, first off, you could vastly increase readability and reduce redundancy with a couple of tweaks here and there; why do you need to store the initial question inside its own variable? Why does a calculator need to indicate that the given response is too large or too small? Additionally, you could make use of a while loop to test for the correct answer iteratively instead of using an if/else block, like so:while(Input != answer && Input > 0) {
//re-prompt the user for the correct answer
//this makes the assumption that the answer is non-negative - sentinel values are important
}