What is wrong with this small for/if loop logic? [closed] - java

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).

Related

How come this loop doesn't print out six numbers? [closed]

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 5 months ago.
Improve this question
package p22_09_24;
public class Lotto2 {
public static void main(String[] args) {
for (int i=0; i <6; i++) {
i = (int)(Math.random()*45)+1;
System.out.println(i);
}
}
}
As you can see, the exit is under the 'for' which means it should loop six times, but the answer is only one num.
public class Random {
public static void main(String[] args) {
for (int i = 0; i < 6; i++)
{
int randomNumber = (int) (Math.random()*45)+1;
System.out.println("Random number is: " + randomNumber);
}
}
}
use different name to store the random number.

Whenever I input bigger number first it doesn't print out anything [closed]

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.

Java is not giving output, sum between two integers [closed]

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

To convert decimal value into its corresponding binary [closed]

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
import java.io.*;
import java.util.*;
public class Binary {
public static void main(String args[]) {
int i = 0, j = 0, num;
Scanner in = new Scanner(System.in);
int arr[] = new int[100];
System.out.println("enter the number");
num = in.nextInt();
while (num != 1) {
j = num % 2;
num = num / 2;
arr[i] = j;
i++;
}
for (i = i; i <= 0; i--) {
System.out.print("The binary number: " + arr[i]);
}
}
}
I wrote this programme to convert decimal input to its corresponding binary value, the programme takes the input but it does not show the output i.e. the binary value. please help
As it was already pointed out, you need to change the condition of
while (num != 1) {
to
while (num > 0) {
since your version is prone to an infinite cycle due to the possibility of num being 2.
Change the for cycle, like this:
for (i = arr.length - 1; i >= 0; i--) {
System.out.print("The binary number: " + arr[i]);
}
but to be able to do this, you need to know how many elements will you need to use, so change the declaration of arr to this
int arr[] = new int[(int)Math.ceil(Math.log(num) / Math.log(2))];
But to be able to do this, you need to initialize num before you declare arr. Code is untested, let me know if there are any typos.
condition should be while(num!=0){ //do calculations}
and change the condition of for loop into for(i=i;i>=0;i--){}
You could use the Integer class and it's static method toBinaryString(int i). This method converts an int into its binary value and returns it as a String.
If I correctly understood what you are trying to achieve, you could just write:
Scanner in = new Scanner(System.in);
System.out.println("enter the number");
int num = in.nextInt();
String binary = Integer.toBinaryString(num);
System.out.print("The binary number: " + binary);

How to populate int array dynamiclly by a given range? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I would like to know how to populate an array dynamically given a start point and end point ? Assuming it's possible ?
my current way (hard coding ints)
int xValues[] = {340,347,348,349,352,355,359,364,369,376,383,392,400,410,421,431,443,455,467,480,492,505,519,532,545,559,572,585,599,612,625,637,648,658,667,675,681,686,690,693,696,697,698,700};
for (int i = 0; i < xValues.length; i++)
{
x=xValues[i];
Desired way
int xValues[] = Range(340, 700); // if possible unit of increment 1 or 10
Thank you and happy coding.
If you are looking for a code that generates random numbers between the given range, the below code is for you
import java.util.Scanner;
import java.util.Random;
public class Insert {
static Scanner input=new Scanner(System.in);
public static void main(String [] args){
Random rand=new Random();
int max,min;
System.out.println("enter the maximum number");
max=input.nextInt();
System.out.println("enter the minimum number");
min=input.nextInt();
int range=max-min+1;
int arr[]=new int[100];
for(int i=0;i<100;i++){
int random=rand.nextInt(max-min+1)+min;
arr[i]=random;
}
for(int i=0;i<100;i++){
System.out.println(arr[i]);
}
}

Categories

Resources