[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
i just started java and i was wondering, if I wanted to negate
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /adv/ - Advice

Thread replies: 11
Thread images: 1
File: ss (2015-12-18 at 07.15.12).png (931 KB, 1241x753) Image search: [Google]
ss (2015-12-18 at 07.15.12).png
931 KB, 1241x753
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++;
}
Thread replies: 11
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.