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 4 years ago.
Improve this question
So I was trying to make the following program:
The user gives two integers as an input (first and last). The program is supposed to give the sum between those two numbers, but when I run the program I don't get an output. However, when I put for the first input a bigger integer than the last input, I get the first input as result. This is my code:
public class TheSumBetweenTwoNumbers {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.println("First: ");
int first = Integer.parseInt(reader.nextLine());
System.out.println("Last: ");
int last = Integer.parseInt(reader.nextLine());
int sum = 0;
while (first <= last); {
sum += first;
first++;
}
System.out.println("The sum is: " + sum);
}
}
your while loop runs without making a change to the block that it contains. you have closed the while loop before it even enters the block.
while (first <= last) {
sum += first;
first++;
}
try this
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
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int num1, num2;
System.out.print("Enter 2 numbers");
num1 = scan.nextInt();
num2 = scan.nextInt();
for (int i = num1; i <=num2; i++) {
System.out.print(i + "");
}
}
}
When I put the first number bigger than the second number, the program doesn't output anything; it is just blank.
Your for loop sets i to be num1 and then checks the condition i <= num2. If num1 (and thus, i) is bigger than num2 the condition evaluates to false, so the for body is never executed.
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 4 years ago.
Improve this question
I am making a coin flip simulator, but I have a problem with the random values. I need to make a random value that changes every time the while-loop starts again.
System.out.println("How many times do I flip the coin?");
int repetition;
int heads = 0;
int tails = 0;
Scanner amount_of_times = new Scanner(System.in);
repetition = amount_of_times.nextInt();
double luck = (Math.random());
do {
if (luck > 0.5)
heads++ ;
else
tails++ ;
repetition --;
} while (repetition > 0);
System.out.println("The amount of heads was " + heads + ", meanwhile the amount of tails was " + tails + ".");
amount_of_times.close();
}
}
You assign a value to luck only once, before the loop. If you want it to get a different value in each iteration, you should move the assignment to it inside the loop:
do {
double luck = Math.random(); // Here!
if (luck > 0.5)
heads++ ;
else
tails++ ;
repetition --;
} while (repetition > 0);
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to write a java program to find the largest square number which is less than or equal to a given number.
For example when I insert 10 as the inputted number, the answer is 9 because 9is the largest square up to 10
I understand loops, but I can't figure out the logic behind this one.
How about something like the following to get you going ...
double x = 20;
double root = Math.sqrt(x);
int t = (int)root;
System.out.println("Root is:" + root + " and answer is :" + t*t);
Here is how to do this with loops
int n=10; //This is your Number
int i = 0;
for(i=n;i>=1;i--)
if((int)Math.sqrt(i)*(int)Math.sqrt(i)==i)
break;
System.out.println(i);
Below is how it works:-
The Loop runs from the n, which is your number, to 1. It then checks whether, the square root of i, which is running through n to 1, is a perfect square. If it is, it breaks the loop, and prints the value of i on the screen.
public static void main(String argv[]){
System.out.println(largestSquareLessOrEqualTo(145));
}
public static int largestSquareLessOrEqualTo(int limit){
int i = 0;
for (; i <(int)Math.sqrt(limit); i++){
}
return(i*i);
}
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
This code is asking for how many numbers the user will input and then it takes each of those. In the end it should return the smallest value. I know about Math.min methods, I'm just struggling with why the logic below doesn't work, it always prints the last input instead the smallest one.
import java.util.Scanner;
public class Ch5_smallestValue {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Input how many numbers and then input each one");
int hMany = sc.nextInt();
int firstNum = sc.nextInt();
int smallest = firstNum;
for (int i = hMany; i > 1; i--){
int input = sc.nextInt();
if (smallest < input){
smallest = input;
}
}
System.out.println("smallest = " + smallest);
}
}
Change (smallest < input) to (smallest > input).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 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 can't seem to figure out why my program is running? Can someone help steer me to the right direction?
import java.util.*;
public class EasyLoops
{
public static void main (String [] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter Number:");
int n = input.nextInt();
while ( n > 0 );
{
System.out.println("Hello World");
n = n - 1;
}
}
}
Remove the ; here:
while ( n > 0 );
That would mean the "content" of the loop is nothing,
and the {} after it are just for fun (valid, but needless)...
It would loop forever (if n>0) because nothing in n changes.
Remove the ; in the while loop otherwise while loop terminates
while ( n > 0 );<- here
while ( n > 0 );
Notice the semicolon. The syntax of a while loop is
while (condition) statement;
or
while (condition) { block }
You wrote the first syntax, and included no statement, so if n is greater than zero you will get an infinite loop (since n will never decrement). If n is less than or equal to zero then the while will not execute and the block below will execute exactly once.
you terminate your loop with ;
while(n>0); // your mistake
{
//your code
}
try this
public static void main (String [] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter Number:");
int n = input.nextInt();
while ( n > 0 )
{
System.out.println("Hello World");
n = n - 1;
}
}
Reomove the ";" the end of while statement. ";" is set only do while Loop not while Loop
while (n > 0)
{
System.out.println("Hello World");
n = n - 1;
}
Thank you