Do-while statements beginner java max-min prompt - java

Hey guys I new to Java and on do-while statements, the question asks me to create a prompt that asks for a max and a min value, then it asks for another value between my max and min values. "The user should be continually be prompted until a number within the range is entered. Im having a hard time wrapping my head around using a do-while statement so some help would be nice thanks! Also try to keep it simple!
package Chapter6Java;
import java.util.Scanner;
public class Chapter6Prompter {
public static void main(String [] args){
int max, min, between;
Scanner input = new Scanner(System.in);
System.out.print("Enter a min value: ");
min = input.nextInt();
System.out.print("Enter a max value: ");
max = input.nextInt();
do {
System.out.print("Enter a value between your min and max values:");
between = input.nextInt();
} while (between != max && between != min);
}
}

Modify your condition in the while as:
while (between >= max || between <= min);
Since you are checking that between is in range you should check if its greater than or equal to minimum value and less than or equal to maximum value.

You want to repeat the loop if the number rentered in not in range
so condition will be - while (between > max || between < min);

Related

The Largest/Smallest Outputs do not showcase the right values

Should receive an output of the largest/smallest numbers within the inputs, what seems to be the problem? I have tried looking it up online, the source code appears to be the same. Thank you for your assistance.
Source code:
package counting.largest.smallest;
import java.util.Scanner;
public class CountingLargestSmallest {
public static void main(String[] args) {
Scanner TheN = new Scanner(System.in);
int counter = 0;
int number;
int smallest = Integer.MIN_VALUE;
int largest = Integer.MAX_VALUE;
while (counter < 10) {
System.out.print("Integer=");
number = TheN.nextInt();
counter++;
if (number < smallest) {
number = smallest;
} else if (number > largest) {
number = largest;
}
}
System.out.println("\nSmallest=" + smallest);
System.out.println("Largest=" + largest);
}
}
Output:
Integer=1
Integer=2
Integer=3
Integer=4
Integer=5
Integer=6
Integer=7
Integer=8
Integer=9
Integer=10
Smallest=-2147483648
Largest=2147483647
there were 3 bugs:
1.
int smallest=Integer.MIN_VALUE;
int largest=Integer.MAX_VALUE;
number=smallest;
number=largest;
Using else if
1: When you want to find a min number keep it Integer.MAX_VALUE and when you want to find max keep it has Integer.MIN_VALUE.
Why for min we Initialize Integer.MAX_VALUE and for max we Initialize Integer.MIN_VALUE?
Say you want to multiple n numbers from the user. So we will declare and initialize mul variable to 1. cause we know any number multiple by 1 will give us the same number.
Whereas if we initialize mul with 0 it will give us 0 only.
So we say 1 for multiplication is identity.
Similarly, for finding Minimum number we use an identity that is Integer.MAX_VALUE.
So that number that less then Integer.MAX_VALUE are saved in Samllest variable.
int smallest=Integer.MAX_VALUE;
Similarly for max we initialize it with Integer.MIN_VALUE SO that number that are greater then Integer.MIN_VALUE are stored in largest variable.
int largest=Integer.MIN_VALUE;
2: You are assigning number variable the value of smallest and largest it should be like smallest=number and largest=number
when you write x=0 it says x is 0 that means x is changed, Similarly when you write number=smallest its number = Integer.MAX_VALUE and smallest is not changing at all.
So you should write like smallest = number, It means smallest is the number and smallest is changing every time condition satisfies.
3: When you write
if(condition){
}else if(conditon){
}
else if is only excuted when first if statement if false and if first condition is true second if is never executed.
if we have descending number then second if will never be excuted which would result in having Integer.MIN_VALUE in largest variable.
And here you want to check for both min and max so we should have indepent if statement for each
if(condition){
}
if(condition){
}
Code:
Scanner TheN= new Scanner(System.in);
int counter=0;
int number;
// changed
int smallest=Integer.MAX_VALUE;
int largest=Integer.MIN_VALUE;
while(counter<10){
System.out.print("Integer=");
number=TheN.nextInt();
counter++;
if(number<smallest){
// changed
smallest= number;
}
if(number>largest){
// changed
largest=number;
}
}
System.out.println("\nSmallest="+smallest);
System.out.println("Largest="+largest);
Output:
Integer=1
Integer=2
Integer=2
Integer=4
Integer=3
Integer=6
Integer=7
Integer=8
Integer=9
Integer=10
Smallest=1
Largest=10

My java programe doesn't loop

So this is my first loop and when i execute it ,it says,
"Enter the price of item 1"
,and when i give it an input, it displays the output without looping even once
import java.util.Scanner;
public class Loop
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
int value;
int sum=0;
int number=1;
while (number > 6);
{
System.out.println("Enter the price of item"+number);
value = input.nextInt( );
sum=sum+value;
number=number+1;
}
double vat=value*0.15;
double total=value+vat;
System.out.println("The Total cost is $"+value+" \n The Value added tax is $"+vat+"\n The total cost including Value Added Tax is $"+total);
}
}
can somebody please help me?
a couple of problems here
int number=1;
while (number > 6);
number is less than six so it would never enter
and
remove the semicolon after the while
You define
int number=1;
And you test
while (number > 6);
{
<some code>
}
The condition is never met since number is 1.
There's also another problem: the semicolon after the while will lead to an infinite loop if you correct just the condition
Fixed code:
while (number < 6) // <- no semicolon & correct comparison
{
System.out.println("Enter the price of item"+number);
value = input.nextInt( );
sum=sum+value;
number=number+1;
}
You have bad while condition, change while (number > 6); to while (number < 6), remmeber to remove the ; after the while
It's because your number is not > 6 in the first place. So it would never satisfy the entering condition of the loop because clearly 1 is not greater than 6. Instead you want to loop while it is < 6. You also need to remove the ; after your while.
So try to replace with that:
int number=1;
while (number < 6)
{
System.out.println("Enter the price of item"+number);
value = input.nextInt( );
sum=sum+value;
number=number+1;
}
Firstly, there is a semicolon after the while statement.
while (number > 6);
Though it is not syntactically wrong but when you want to loop, the semicolon must be omitted.
Secondly, number is initialized to 1 and while loop checks whether 1 > 6, which is false.

print entered integer from input integer to integer

I am trying to obtain two integers as input, and then use these to output another list of integers, using the input as arguments in a loop.
In my code, if I enter low as 1, and high as 10 I expect my output should be the integers 1 through 10. However my code prints the value 1 ten times. I have tried different loops with no luck. Can someone please point out why my output is not as expected?
import java.util.Scanner;
public class test1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the low number: ");
int low = input.nextInt();
System.out.print("Enter the high number: ");
int high = input.nextInt();
//int num1 = low
//int num2 = high
System.out.println("Decimal");
for(int i = low ; low <= high; low++)
System.out.println(i);
}
}
So the issue is that in your for loop you are incrementing the value of low with low++ but you're printing i which you only set to the value of low at the beginning; i doesn't get reset to the value of low on every iteration in your for loop.
So, try changing low++ to i++ and see if that makes a difference ;)
You'll also want to change low <= high to i <= high as we are now incrementing i.

Smallest and Second Smallest of a number list

Problem 2
Write your program in the file TwoSmall.java.
Input a set of positive integers, ending with -1 as a sentinel. Print the smallest and second smallest of these numbers in that order.
You may assume there are always at least two numbers before the -1; you do NOT have to check for this being true.
public class TwoSmall{
public static void main(String [] args){
int min;
int secondMin;
int sentinel = -1;
System.out.println("Enter atleast two numbers");
int input = IO.readInt();
min = input;
secondMin = input;
do{
if(input < min){
secondMin = min;
min = input;
}
else if (input < secondMin) {
secondMin = input;
}
input = IO.readInt();
} while(input!=sentinel);
System.out.println(min);
System.out.println(secondMin);
}
}
This is not working? Could someone tell me what is wrong with this program?
It's because you set both min and secondMin equal to the first entered value. Unless the numbers entered are less than that value, you'll have an issue. Try reading in both those numbers separately.
min = IO.readInt();
secondMin = IO.readInt();
Also, rethink your logic. You might be getting a backwards result. A way to work yourself around this problem is to read the first two integers, then assign them to min and secondMin accordingly. Otherwise, if the user enters 4,3,2 -- the result will say that 3 is the lowest and that 2 is the second lowest - which is incorrect.
Another option, as mentioned by #vandale is to initialize both of them to INTEGER.MAX_VALUE. If you do this, you eliminate the need to read in initial values for min and secondMin.

Unsure what the issue is

I'm really new to this whole programming thing, and I'm trying to wrap my head around why the loop ends abruptly and does not continue to the final if statement. Can you guys help me figure out whats wrong?
import java.util.Scanner;
public class FunnyAverage {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("How many values to read? ");
int top = in.nextInt();
System.out.print("Enter Value: ");
int one = in.nextInt();
int number = 1;
int sum = 0;
sum = sum + one;
while (number <= top) {
if (one % 6 != 0 && one % 17 != 0) {
System.out.print("Enter Value: ");
one = in.nextInt();
number++;
} else if (one % 6 == 0 && one % 17 == 0) {
System.out.print("Enter Value: ");
one = in.nextInt();
number++;
}
}
if (sum / top != 0) {
System.out.print("Average: " + sum / top);
}
System.out.print("None Divisible");
}
}
The final if() condition executes if you give the right input values. I ran your code and gave the below inputs to execute the final if() statement.
How many values to read? 1
Enter Value: 1
Enter Value: 1
Average: 1None Divisible
I dont understand what are you trying in the code, but there are many things missing like i assume you want to capture the sum of the input numbers, but sum is not used in the while loop.
Looks like you end up in the non-present else case (within the while loop). Consequently, number isn't increased and you are stuck in the while loop.
Try reading one within the while loop. This way the user will be prompted to enter a new number in each loop.
Otherwise you will be stuck in the while loop once the user enters a number that isn't conform with your checks.

Categories