[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Can someone help me with my code, I'm trying to create a
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 22
Thread images: 1
File: java[1].jpg (107 KB, 1280x768) Image search: [Google]
java[1].jpg
107 KB, 1280x768
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.
>>
>>51660300
while (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...
>>
>>51663369
x = 5
while True:
x -= 1
print x + 1
if x < 1:
True = False
>>
Are people on /g/ really this bad at coding?
>>
>>51663162
Scanner 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
}
Thread replies: 22
Thread images: 1

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.