while-loop

Endless loop while using “try and catch ” block inside a “while loop”

核能气质少年 提交于 2019-12-29 09:22:10
问题 My program has an endless loop, when I use try and catch block in a while loop . import java.util.*; class Try { public static void main(String args[]) { Scanner sc=new Scanner(System.in); while(true) { try { System.out.println("Enter a no "); int s=sc.nextInt(); } catch(Exception e) { System.out.println("Invalid input try again"); } } } } When I input an integer, it runs fine and asks for another input, but when I input a char, it goes for endless loop. Why is this so? 回答1: Your program

Endless loop while using “try and catch ” block inside a “while loop”

谁说我不能喝 提交于 2019-12-29 09:21:45
问题 My program has an endless loop, when I use try and catch block in a while loop . import java.util.*; class Try { public static void main(String args[]) { Scanner sc=new Scanner(System.in); while(true) { try { System.out.println("Enter a no "); int s=sc.nextInt(); } catch(Exception e) { System.out.println("Invalid input try again"); } } } } When I input an integer, it runs fine and asks for another input, but when I input a char, it goes for endless loop. Why is this so? 回答1: Your program

What happens when one places a semi-colon after a while loop's condition?

﹥>﹥吖頭↗ 提交于 2019-12-29 07:35:11
问题 I've come across a situation like this a few times: while (true) { while (age == 5); //What does this semi-colon indicate? //Code //Code //Code } The while(true) indicates that this is an infinite loop, but I have trouble understanding what the semi-colon after the while condition accomplishes, isn't it equivalent to this?: while (age == 5) { } //Code //Code In other words, does it mean that the while loop is useless as it never enters the block? 回答1: while (age == 5); // empty statement is

How do I get this code to stop input when the sum exceeds 100 and still preform the sum and average?

北城余情 提交于 2019-12-29 02:01:09
问题 I have been at this for hours!!!! An update to the assignment states that we need to stop the user input when their values exceed 100. Without rewriting the whole thing, how do I "loop" this in? I know the code is method heavy but it was a requirement for the assignment. I think my brain is just java-soup! Any help would be AWESOME :) public static void main(String[] args) { //Use Main Method for gathering input float input = 1; // Declare variable for sum float theSum = 0; // Declare

While loop one-liner

ε祈祈猫儿з 提交于 2019-12-28 18:09:25
问题 Is it possible for to have a python while loop purely on one line, I've tried this: while n<1000:if n%3==0 or n%5==0:rn+=n But it produces an error message: Invalid Syntax at the if statement 回答1: When using a compound statement in python (statements that need a suite, an indented block), and that block contains only simple statements , you can remove the newline, and separate the simple statements with semicolons. However, that does not support compound statements. So: if expression: print

bash script, create array of all files in a directory

穿精又带淫゛_ 提交于 2019-12-28 11:45:11
问题 I have a directory myDir of many .html files. I am trying to create an array of all the files in the directory so I might be able to index the array and be able to refer to particular html files in the directory. I have tried the following line: myFileNames=$(ls ~/myDir) for file in $myFileNames; #do something but I want to be able to have a counter variable and have logic like the following: while $counter>=0; #do something to myFileNames[counter] I am quite new to shell scripting and am

jQuery Looping and Attaching Click Events

蹲街弑〆低调 提交于 2019-12-28 07:01:28
问题 Okay I've tried multiple things and looked for answers to this and I can't seem to get it to work. What I'm trying to do is there are some dynamically generated images, so I don't know how many images there will be at any time. Each image has some associated information that I store in a seperate div. What I want to do is attach a click event to each image that will unhide the div that has the associated content in it. I've tried looping with both a for loop and a while loop, to no avail.

While, Do While, For loops in Assembly Language (emu8086)

自作多情 提交于 2019-12-28 04:03:44
问题 I want to convert simple loops in high-level languages into assembly language (for emu8086) say, I have this code: for(int x = 0; x<=3; x++) { //Do something! } or int x=1; do{ //Do something! } while(x==1) or while(x==1){ //Do something } How do I do this in emu8086? 回答1: For-loops: For-loop in C: for(int x = 0; x<=3; x++) { //Do something! } The same loop in 8086 assembler: xor cx,cx ; cx-register is the counter, set to 0 loop1 nop ; Whatever you wanna do goes here, should not change cx inc

Whats wrong with this while loop? [duplicate]

人走茶凉 提交于 2019-12-28 01:33:06
问题 This question already has answers here : Semicolon at end of 'if' statement (18 answers) Closed 2 years ago . boolean r = false ; int s = 0 ; while (r == false) ; { s = getInt() ; if (!(s>=0 && s<=2)) System.out.println ("try again not a valid response") ; else r = true ; } The text never displays itself even when a 3 or a 123 is entered and the loop never terminates. Whats wrong here? 回答1: You have a semicolon after the condition. When you use braces to specify a block for your while you don

How do I exit a while loop in Java?

我只是一个虾纸丫 提交于 2019-12-27 17:06:09
问题 What is the best way to exit/terminate a while loop in Java? For example, my code is currently as follows: while(true){ if(obj == null){ // I need to exit here } } 回答1: Use break: while (true) { .... if (obj == null) { break; } .... } However, if your code looks exactly like you have specified you can use a normal while loop and change the condition to obj != null : while (obj != null) { .... } 回答2: while(obj != null){ // statements. } 回答3: break is what you're looking for: while (true) { if