Hi, Im very new to java (about a month) with a useless lecturer who has taught us nothing. Everything I have learnt so far as been on my own at home trying things.

I did a question, but I wanted to try adding things to it to learn so started experimenting. Im sure it will look a mess to some here but like I said I have very limited knowledge and Im new. My problem is the final if statement seems to run no matter what the condition - its only supposed to show how many negative numbers were entered if the previous if statment is triggered and asks the user if they want to see this (and this is only triggered if any negative number were entered in the program at all anyway, that bit seems to work).

public class QuestionTwo
{
public static void main(String[] args)
{
int Min=0, Min2, Max=0, Result=0, Check;
boolean Ask=false;
do
{
Check=0;
if(Min>Max)
{
	System.out.println("Please make sure the Minimum number entered is smaller than the larger number entered");
	System.out.println();
}
else
	;
	System.out.println("Enter a Minimum Number: ");
	Min = SavitchIn.readLineInt();
while (Min<0)
{
if(Min<0)
{
	System.out.println("Please make sure the Minimum Number entered is positive: ");
	Min = SavitchIn.readLineInt();
	Check=Check+1;
}
else 
	;

	; 
}
	System.out.println("Enter a Maximum Number: ");
	Max = SavitchIn.readLineInt();
while(Max<0)
{
if(Max<0)
{
	System.out.println("Please make sure the Maximum Number entered is positive: ");
	Max = SavitchIn.readLineInt();
	Check=Check+1;
}
else
	;
	;
}
}
while(Min>Max);
Min2=Min;
for (Min=Min; Min<=Max; Min++)
{
	Result=Result+Min;
}
System.out.println("\nThe sum of all numbers between "+Min2+" and "+Max+" = "+Result);
System.out.println();
if(Check>0)
{
	System.out.println("Would you like to see how many negative numbers were incorrectly entered?\nt=Yes f=No\n");
	Ask = SavitchIn.readLineBoolean();
}
else
	;
if(Ask=true)
	System.out.println(+Check+ " Negative number(s) were entered");
else
	System.out.println("Goodbye");
}// main
}          // QuestionTwo

The program should;

Ask for a minimum number, and use a loop to check of it is negative (count each negative number entered)
then
Ask for a maximum number, with the same conditions as the minimum.
It will then check if the minimum is larger than the maximum, and if it is start the do while loop again, this time with a warning.
It then counts the sum of all the numbers with a for loop and displays the answer.
Finally the final two if statements I have already described should run. Any one know how to fix this (Ive been trying for a while)?

Edit: Just realised you will need SavitchIn.class to run this, as its what we are using to learn. I found a link to it here http://www.cs.miami.edu/~duncan/spring03/csc220/classes/SavitchIn.class

Thanks, Michael

Edited 6 Years Ago by mabrookes: n/a

Well I just made myself look stupid :$ Last night when I was working on this, the last thing I changed before going out never got tested and I forgot about it.

Although that change didnt work, it was only one step away from being the solution and after deciding to give it one last look over it was easy to notice a mistake there, after changing it the code now works. It just needed changing from if(Ask=true) to if(Ask==true). That will teach me to give up half way through trying something lol

Just out of curiosity, I originally tried to allow the user to type yes or no before realising I just dont know how to do this and stuck with a boolean type, is there a simple way of doing this and if so, could you just let me know what I need to look up to learn this.

Thanks anyway,

Michael

Just out of curiosity, I originally tried to allow the user to type yes or no before realising I just dont know how to do this and stuck with a boolean type, is there a simple way of doing this and if so, could you just let me know what I need to look up to learn this.

See if the user typed the word "yes" in any case:

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter yes to do stuff!");
String input = keyboard.next();
if (input.equalsIgnoreCase("yes")){
  doSomeStuff();
}

Also, check out my changes to your code.

public class QuestionTwo
{
public static void main(String[] args)
{
int min=0, min2, max=0, result=0, check;
boolean ask=false;

System.out.println("Enter a Minimum Number: ");
min = SavitchIn.readLineInt();

while (min<0)
{

//I took out your if statement, it was redundant. Since you
//made it into this loop, you already know min is < 0.
System.out.println("Please make sure the Minimum Number entered is positive: ");
	min = SavitchIn.readLineInt();
	check++; //is the same as saying check = check + 1;
}

System.out.println("Enter a Maximum Number: ");
max = SavitchIn.readLineInt();
while(max<0)
{

	System.out.println("Please make sure the Maximum Number entered is positive: ");
	max = SavitchIn.readLineInt();
	check++;
}

while(min>max);
min2=min;

//This makes no sense. You set min equal to itself.
//It is already equal to itself. What are you trying to do here?
for (Min=Min; Min<=Max; Min++)
{
	Result=Result+Min;
}

//If you were trying to add up the sum of the numbers
//between min2 and max, inclusive, it would be
for (int i = min2; i < max+1; i++){
result+=i; //Same as saying result = result + i;
}

System.out.println("\nThe sum of all numbers between "+min2+" and "+max+" = "+result);
System.out.println();
if(check>0)
{
	System.out.println("Would you like to see how many negative numbers were incorrectly entered?\nt=Yes f=No\n");
	ask = SavitchIn.readLineBoolean();
}

if(ask) //same as saying if (ask==true)
	System.out.println(+Check+ " Negative number(s) were entered");
else
	System.out.println("Goodbye");
}// main
}          // QuestionTwo

You might want to look into how to correctly use if/else statements; some of your if/else statements looked wrong. If you don't use brackets {}, then only the first line after the else statements gets executed (as part of that if/else statement). And putting ';' on a separate line has no effect.

Also, check out my changes to your code.

public class QuestionTwo
{
public static void main(String[] args)
{
int min=0, min2, max=0, result=0, check;
boolean ask=false;

System.out.println("Enter a Minimum Number: ");
min = SavitchIn.readLineInt();

while (min<0)
{

//I took out your if statement, it was redundant. Since you
//made it into this loop, you already know min is < 0.
System.out.println("Please make sure the Minimum Number entered is positive: ");
	min = SavitchIn.readLineInt();
	check++; //is the same as saying check = check + 1;
}

System.out.println("Enter a Maximum Number: ");
max = SavitchIn.readLineInt();
while(max<0)
{

	System.out.println("Please make sure the Maximum Number entered is positive: ");
	max = SavitchIn.readLineInt();
	check++;
}

while(min>max);
min2=min;

//This makes no sense. You set min equal to itself.
//It is already equal to itself. What are you trying to do here?
for (Min=Min; Min<=Max; Min++)
{
	Result=Result+Min;
}

//If you were trying to add up the sum of the numbers
//between min2 and max, inclusive, it would be
for (int i = min2; i < max+1; i++){
result+=i; //Same as saying result = result + i;
}

System.out.println("\nThe sum of all numbers between "+min2+" and "+max+" = "+result);
System.out.println();
if(check>0)
{
	System.out.println("Would you like to see how many negative numbers were incorrectly entered?\nt=Yes f=No\n");
	ask = SavitchIn.readLineBoolean();
}

if(ask) //same as saying if (ask==true)
	System.out.println(+Check+ " Negative number(s) were entered");
else
	System.out.println("Goodbye");
}// main
}          // QuestionTwo

You might want to look into how to correctly use if/else statements; some of your if/else statements looked wrong. If you don't use brackets {}, then only the first line after the else statements gets executed (as part of that if/else statement). And putting ';' on a separate line has no effect.

Hey thanks for the reply. I’m just about to try the suggestion for inputting yes in.

The code will look a little odd, but some of that was on purpose, I was just getting used to going through it and when I was putting what may look like random “;” (which is the same as saying do nothing as far as I was led to believe) it was so I could go through the code and know more clearly what is happening (so for example, I know it is saying "if(this) then (this) else "; - do nothing"" if you know what I mean). I know you can omit these things but I’m just trying to get used to it first.

I had a look through to find where I had omitted brackets and I can’t see any at first glance, they all seem to be working for me as well. Was there one in particular or were you just giving advice (I do often to forget to do it initially but normally correct myself).

"This makes no sense. You set min equal to itself.
It is already equal to itself. What are you trying to do here?"

I was going by what few notes we have for for loops. It says "for ( Initialising_Action; Boolean_Expression; Update_Action )" - I needed the initialising action of which I just used the Min that was already set rather than use a something new (you add "i" into the example you show, I skipped that part by just using Min but was unsure how to go about it). Could I set that initialising action other than saying Min=Min, can you just put =Min or something like that?

The first if statement you took out was part of the do while loop. I wanted the first run through to not give a warning about the minimum needing to be bigger than the maximum number entered, but then if the user does this, it would run the do section again and this time the if statement would give the warning.

I know there are general problems and its a bit everywhere, I’m going to edit my for loop to be like yours (mine did work, as does the code in general now, try compiling it and running it) but I want to get into good habits at the same time :)

And the "min2" was added purely so I could put it in the final answer, as I was using "Min" in the for loop so I thought it would be different to what I needed (again, silly way around but im just trying out things and learning).

Thanks for taking the time to answer and give advice, I appreciate it.

Edited 6 Years Ago by mabrookes: n/a

This article has been dead for over six months. Start a new discussion instead.