i just started java and i was wondering, if I wanted to negate this
index =0;
int[]array = new int[10];
while((!salaries==-1) && index<salaries.length){
I want to let up to 10 users enter their salaries, and exit the loop using -1. would this work? is my exclamation mark used correctly? ty O_O
>>16580708
Yes lol for sure. If you wanted to negate what?
Anyway. Why would you want to exit with -1?
>>16580724
I'll have the user enter into salary, and they can enter -1 to exit the loop.
also i think it should be while(!salaries[index]==-1)
while (index < 10) {
// read salary
index++;
}
>>16580732
well, I want it to also let the user leave the loop when they want, by entering -1. so I want the while loop to only work when the user has not entered -1 for salaries[index]. I just wanted to know if I placed my exclamation point properly.
>>16580731
You could use an variable to store the users input and if that input is equal to -1 the exit the program. Also, have count variable that starts at one and increments until it reaches 10. Include that in your check to see if you want to exit or not. So, you'll exit your program if the input equals -1 or if your count is greater or equal to 10 (as they've reached the maximum number of allowed inputs)
>>16580732
Inside the loop
if( salaryInput == -1){
break;
}
>>16580742
Or I could do boolean exitVar = false,
while(exitVar==false &&index<salaries[index].length){
if (salaries[index]==-1){
exitVar=true;
}
salaries[index] = enter name
if (salaries[index]==""){
print error
}else{
index++
}
}
Does this work? wanted to do it without break.
>>16580708
>!salaries==-1
What the fuck?
>>16580731
>also i think it should be while(!salaries[index]==-1)
! gives you a boolean, not int. Use !=
salaries[index] != -1
>>16580772
thanks
>>16580754
index=0;
while(index < 10)
{
//get input
if(input == -1)
System.exit(0);
salaries[index] = input;
index++;
}