Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
public class LockPicker {
public static void main(String[] args) {
System.out.println ("Picking the lock...");
boolean lock = false;
while (lock==false)
{
int counter = 0;
int number = (int)(Math.random() * 99 + 1);
System.out.println(number);
if (number!=55){
lock = false;
++counter;
}else{
lock = true;
System.out.println("That took "+counter+" tries!");
}
}
}
}
Hello,
I have been set a task where I have to write a program that will generate random numbers between 10 and 99. The program
should continue to repeat until the number 55 is created. The program should output how
many attempts it made to get 55.
It works fine, and stops when it reaches 55. However, the counter always remains at 0. It never adds 1 onto the variable counter.
Move the int counter = 0; line before the while loop.
Everytime the loop runs, you are initializing the counter variable to 0, that is why it always remains 0.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
The below code is not giving any error. After debugging i found it is not going inside the first loop.
package string_pattern;
public class Naive {
public static void main(String[] args)
{
String pattern="This is a text book";
String text="text";
child obj=new child();
obj.search(pattern, text);
}
}
class child{
public void search(String pat,String text)
{
int m,n;
m=pat.length();
n=text.length();
System.out.println("i am outside loop");
for(int i=0; i<= (n-m); i++)
{
int j;
System.out.println("i am inside first loop");
for(j=0;j<m;j++)
{
if(text.charAt(i+j) != pat.charAt(j))
{
System.out.println("OVER HERE");
break;
}
if(j==m)
System.out.println("pattern found at pos" + i);
}
}
}
}
You do the following:
m=pat.length();
n=text.length();
The problem is that text has a length of 4, while pat is longer. So when you do the loop like this -> for(int i=0; i<= (n-m); i++), you end up with a negative number. Since 0 is greater than any negative number you never enter the loop.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Why an int becomes zero when it exceed the int limit in java?
Eg:
import java.util.Scanner
public class MyClass{
public static void main(Stings [] args){
Scanner sc = new Scanner(System.in);
int x = sc.nextInt(); // enter 50 or higher.
int answer = 1;
while(x>0){
answer = answer*x;
x--;
}
System.out.println(answer);
}
}
At some point answer becomes 0 (integer overflow) and stays 0.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I tried to write an primenumber generator. The method calcall() should return prime numbers (2,3,5,7...). Unfortunately I get the error, that the method doesn't returns an integer, wich I don't understand. Here is my code:
package primenumber;
public class primecalc {
public static int calcall(int a) { //actual generator
int konstante = a; //is this number a prime num?
int divisor = a-1; //divisor
int var1 = 0; //variable = 0
while(divisor>1) {
int quotient = konstante%divisor; //calc modulo
if(quotient == 0) { //if modulo==0 switch var1 to
var1++; //1 -> no primenumber
break; //stop calculating
} else { //else keep calculculating
divisor--; //until divisor <= 1
}
}
if(var1==0) { //if var1 still 0;
return konstante; //is a primnumber ->
} //return konstante
}
public static void main(String[] args) { //main function
int number = 3; //start with 3
while(True) { //(i'll add 2 manually)
System.out.println(calcall(number)); //print the prime number
number++; //increase number by one
}
}
}
The error is:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
This method must return a result of type int
at primenumber/primenumber.primecalc.calcall(primecalc.java:5)
at primenumber/primenumber.primecalc.main(primecalc.java:28)
What is wrong?
The gray lines on the code you posted are being ignored by the compiler.
The use of /* and */ makes everything between these seen by the compiler as comments. And that is why those lines are grayed out. If you want to comment on the same line as the code, I'd advise you to use //.
Also, it is common practise to use multi-line comments only to describe functions and place them just above the header of the function. Any other comments should be short, concise and describe functionality. Good variable names and well written code should do most of the explaining, and single line comments should be used when it's a bit harder toperceive what's going on.
Cheers
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Why I try to use the loop to display the numbers I had put into the ArrayList, it says Unreachable code what am I doing wrong.
private void nOther1() {
ArrayList<Integer> multiples = new ArrayList<Integer>();
int n = 1;
while (1 <= 100) {
multiples.add(n);
n++;
}
// UNREACHABLE CODE
for (int num : multiples) {
System.out.println(num);
}
}
Because
while (1 <= 100)
will never terminate since 1 is always <= 100, and the compiler knows that.
See Chapter 14.21. Unreachable Statements for detailed explanation.
This condition while(1 <= 100){ is always returning true
it would be the same as you do while(true){ so all behind that point is code that NEVER will be executed..
I think you wanted trying instead:
while(n <= 100){
multiples.add(n);
n++;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
public class Main {
public static void main(String [] args) {
int i = 0;
do {
System.out.println(i);
i++;
} while (i==3);
}
}
// Outputs 0
Why do iterations stop at zero? while(i==3) is a condition, which tests for equality of i to 3. But then, even after incrementing the value of i as i++ why the output is only 0?
Because it'll print i, which is 0, then will increment it, and won't loop since 3 != 1:
do {
System.out.println(i); //i is 0, will print 0
i++; //i is now 1
} while (i==3); //false
Read The while and do-while Statements to better understand how the do-while loop works.
I'm carefully assuming that you meant to write while(i != 3);, if that's the case, your program will loop and will print 0 1 2.
Your condition would be i!=3. Change to while(i!=3). You have mistakenly put the wrong condition in the while loop. You have said it to loop the while only when i==3 otherwise to exit the loop. So you need to change it to
do {
System.out.println(i);
i++;
} while (i!=3);
Hope this clarifies!
Because you increment after printing.