Java comparing 10<-5 gets true [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 4 years ago.
Improve this question
i'm learning java and got a problem, I have an max/min exercise and tried the code bellow, for positive numbers it work fine, but when i try negative numbers the things got messy.
If you enter 5 you get min = 5 and max = 5, if you enter 10 after, you get min = 5 max = 10, so far so good, but if you type -5, you get min = -5 and max = -5, and things still get weirder, if you type 7, you get min =-5 and max = 7.
Can someone explain why this happens?
public static void main(String[] args) {
int ctrl;
int min = 0;
int max = 0;
boolean hasNextInt;
Scanner scanner = new Scanner(System.in);
int counter = 0;
while (true) {
System.out.println("Enter your number:");
hasNextInt = scanner.hasNextInt();
if(hasNextInt) {
ctrl = scanner.nextInt();
if(counter == 0) {
min = ctrl;
max = ctrl;
counter ++;
}
if(min>ctrl)
min = ctrl;
if(max<ctrl);
max = ctrl;
System.out.println("Minimum Number entered: " + min);
System.out.println("Maximum Number entered: " + max);
System.out.println("");
scanner.nextLine();
}else {
System.out.println("Invalid Number. Program stop working.");
break;
}
}
scanner.close();
}

if(max<ctrl);
max = ctrl;
That indentation is highly misleading.
Use an IDE that does code formatting. What you really wrote is
if(max<ctrl){}
max = ctrl;
Also avoid if/else/for etc without {}.

Related

public static double verifyDouble function not returning [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
I have this function, which I made, but it isn't functioning.
Does anyone know, why it doesn't return my error message if I put 5 or a higher number?
public static Map randomMap(Homes[] spots){
System.out.println("Please enter the desired probability");
System.out.println("NOTE: must be written in decimals, so 50% is 0,5");
double homeProbability = verifyDouble(input.nextDouble(), 0, 1);
System.out.println("Please enter the average amount of people in a home");
int avgPeople = verifyInt(input.nextInt(), 1);
int randomInt1 = util.randomInt(20)+1;
int randomInt2 = util.randomInt(20)+1;
return new Map(randomInt1, randomInt2, spots, homeProbability , avgPeople);
}
input.Double reads the input from keyboard.
public static double verifyDouble(double num, double min, double max){
do{
if(num < min && num > max){
System.out.println("Please enter a number between " + min + " and " + max + ":");
num = input.nextDouble();
}
}while(num < min && num > max);
return num;
}
The condition, num < min && num > max does not make sense as num can not be less than min and greater than max at the same time. It should be:
num < min || num > max
which means num is either less than min or greater than max.

How to link one with another parts [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 3 years ago.
Improve this question
I have this homework. I'm having a hard time linking the first part with the second. This is what I have. I know I'm missing something at some point to indicate that from the number input it should add the next 100 numbers.
package test;
import java.util.Scanner;
public class Ex216 {
public static void main(String[] args) {
// Write a program in Java that reads an integer from the keyboard and makes the sum of the next 100 numbers, showing the result on screen
Scanner myInput = new Scanner(System.in);
int =a
int sum;
System.out.print("Enter first integer: ");
a = myInput.nextInt();
for (int n = a; n <= 100; n++)
System.out.printf("Sum = %d\n", sum);
}
}
This is what is casing me the trouble.
first of all,int =a is not a valid expression. it should be int a; then as you want to add next 100 number from the given value, you need to add those values into sum, such as sum = sum+number.
Here is a code snippet:
Scanner myInput = new Scanner(System.in);
// correct declaration
int a;
// initialize sum with zero.
int sum=0;
System.out.print("Enter first integer: ");
a = myInput.nextInt();
//for simplicity,start value n from a and loop until n reaches a+100.
for (int n = a; n <= 100+a; n++) {
sum = sum + n;
}
System.out.println("Sum = "+ sum);

Why does my sentinel value doesn't work and I can enter unlimited integers but get nothing? [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`m trying to write a code that has a sentinel value 0 . It prompts the user to enter numbers and it finds positives negatives and sum of all the entered numbers. I cannot understand why after I enter sentinel value 0 it does not print anything . Is there something wrong with my while loop ? thank you !
I tried many things but still couldn`t see the reason .
package Csc220Study;
import java.util.Scanner;
public class S51 {
public static void main(String [] args){
System.out.println(" enter an integer , the input ends if it is 0:");
Scanner input = new Scanner(System.in);
int sum =0;
int positives=0;
int negatives=0;
int count=0;
int x = input.nextInt();
while(x!=0){
sum=+x;
if(x<0){
negatives++;
}else if(x>0){
positives++;
}
count++;
}
System.out.println("the sum is :"+sum);
System.out.println("positive number here :"+positives);
System.out.println("negative numbers are :"+negatives);
change your while loop part to this:
int x = input.nextInt();
while(x!=0){
sum+=x;
if(x<0){
negatives++;
}else if(x>0){
positives++;
}
count++;
x = input.nextInt();
}
when you are taking input a non zero integer it is in an infinite loop as value of x is never changing.

Problems with array. Java [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
I started learning java yesterday and this is the first time i'm using array. This is the code
import java.util.Scanner;
public class array
{
public static void main(String[] args)
{
int num[];
num = new int[5];
Scanner input = new Scanner(System.in);
int i;
System.out.println("Insert 5 numbers:");
for(i = 0; i < 5; i = i + 1);
{
System.out.print("Insert the " + i + "° number: ");
num[i] = input.nextInt();
}
System.out.print("The numbers you entered are: ");
for(i = 0; i < 5 ; i = i + 1)
{
System.out.println(num[i] + " ");
}
}
}
When i try to run it i get this problem:
Insert 5 numbers:
Insert the 5° number: 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at array.main(array.java:14)
Two things.
You have a semicolon at the end of your loop. That will cause the loop to run until i = 5 and you're kind of stuck with that value now. Remove it.
i will remain 5 after the first loop for the same reason as above. Declare and initialize i inside of your for statement.
for(int i = 0; i < 5; i = i + 1) {
// the rest of your block
}

Program where user enters 2 inputs and a limit. It finds the sum of the multiples. please check [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
The user must enter x, y, and the limit. The program must find the multiples of these numbers that are below the limit that the user set themselves. The program adds up all the multiples and prints just that number at the end, and not all the multiples. For some reason it just isn't working for me and I can't figure it out.
import java.util.Scanner;
public class SumMultiples.java {
public static void main (String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter x value: ");
int x = scanner.nextInt();
System.out.print("Enter y value: ");
int y = scanner.nextInt();
System.out.print("Enter limit value: ");
int limit = scanner.nextInt()
int sum = 0;
for (int i = 0; i < limit; i++) {
if (((i % x) == 0 || ((i % y) == 0))) {
sum += i;
}
}
}
System.out.println(sum);
}
From copying your code into Eclipse:
Firstly you are missing a ; when you are getting the input for limit.
Secondly your System.out.println(sum); is outside of your main method, it should be at the end of the method i.e. after your for loop.
Thirdly, you have named your class SumMultiples.java. This is not a valid name, rename it to SumMultiples.
You should be getting compile errors and your IDE should help you work this out. If you aren't using an IDE, please do so as it will help you debug these issues and help you get familiar with Java.

Categories