Sorry i'm stuck on converting this to do while loop can anybody help me please.
int sum = 0;
int num;
System.out.print("Enter number: ");
num = sc.nextInt();
// Store the user input into variable num
// Complete the for loop to start from 1
// and end at num
for (int i = 1 ; i <=num ; i++)
sum += i;
System.out.println("The sum is " + sum);
and this is my do while loop
int sum = 0;
int num;
int i = 1;
do {
sum += i;
System.out.print("Enter number: ");
num = sc.nextInt();
i++;
} while (i <= num);
System.out.println("The sum is " + sum);
int sum = 0;
int num;
int i = 0;
System.out.print("Enter number: ");
num = sc.nextInt();
do{
sum += i;
i++;
}
while ( i <=num );
System.out.println("The sum is " + sum);
Initialize i to zero, since do-while does first before checking, as opposed to for that checks first before doing. And your do-while will work the same as your for.Or else your do-while will have a sum of 1 even if your num is 0. As opposed to your for that will have sum=0 if num is 0.
In the first case, you enter num value outside the for, the second case you do it on the do/while. So I supposed you want to do this:
//Variables
int sum = 0;
int num;
int i = 0;
//Select num
System.out.print("Enter number: ");
num = sc.nextInt();
do {
sum += i;
i++;
} while (i <=num);
System.out.println("The sum is " + sum);
First you may have to study the basics of the loops a bit more. The difference between while loop and do-while loop is the place where the condition is being checked. Do-while loop checks the condition after executing the code block. And the while loop doesn't. It checks before the execution.
int num = sc.nextInt();
int i = 1;
int sum = 0;
do{
sum += i;
i++;
}while(i <= num); //check condition after the running do block
System.out.println(sum);
**This depends on your input. That means if your input is zero the answer will be wrong. Because your i = 1 and sum becomes equals to 1.
Related
I am so very new to coding and forgive me if this very basic question has been answered however when searching I still cannot see what is missing from my code. I am simply trying to get the sum of 5 integers. I can get it to print the integers however it just runs after that and never gives the answer. What is it waiting for me to do or what is my code missing? I have no errors so I am just not seeing what is wrong.
System.out.print("Please enter 5 integers: ");
int arr = input.nextInt();
int sum = 0;
for(int a = 1; a <= 100; a++){
arr = input.nextInt();
}
sum = sum + arr;
System.out.println("The sum of 5 integers is: " + sum);
Try:
System.out.print("Please enter 5 integers: ");
Scanner input = new Scanner(System.in);
int sum = 0;
for(int a = 1; a <= 5; a++){
sum += input.nextInt();
}
System.out.println("The sum of 5 integers is: " + sum);
package HW2_Min_Max;
import java.util.Scanner;
public class HW2_Min_Max {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
System.out.println("Please input a positive interger that indicates number of positive intergers ");
int number = myScanner.nextInt();
while (number <= 0) {
System.out.println("Please input interger");
number = myScanner.nextInt();
}
int i=1; //i is to store current iteration
int sum=0; //sum is to store sum of the input
int x; //x is to store the user input
while (i <= number){
System.out.println("Please input a positive interger ");
x = myScanner.nextInt();
sum = sum + x;
i++;
}
int average = sum/number;
System.out.println("The average is " + average);
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
if (number < min){
min = number;
}
if (number > max) {
max = number;
}
System.out.println("The minimum value is " + min);
System.out.print( "and the maximum value is" + max);
}
}
}
1.^ this is where i am getting my problem, on the very last brace in Netbeans i am getting an error that says "class, interface, or enum expected" but i have no idea why. Excuse my ignorance as I am a very fresh beginner with java, let alone programming.
For the first part, I would suggest you use a do-while loop to test for the number of numbers. Like,
int number;
do {
System.out.println("Please input a positive integer that "
+ "indicates number of positive integers ");
number = myScanner.nextInt();
} while (number <= 0);
Then you need to set min and max in your loop (or store all the values you read). I would prefer Math.max and Math.min. I would also count from 0. Like,
int i = 0; // i is to store current iteration
int sum = 0; // sum is to store sum of the input
int x; // x is to store the user input
int max = Integer.MIN_VALUE; // store the max user input
int min = Integer.MAX_VALUE; // store the min user input
while (i < number) {
System.out.println("Please input a positive integer ");
x = myScanner.nextInt();
if (x > 0) { // make sure it's a positive integer
min = Math.min(min, x);
max = Math.max(max, x);
sum += x;
i++;
}
}
int average = sum / number;
System.out.println("The average is " + average);
System.out.println("The minimum value is " + min);
System.out.println("and the maximum value is " + max);
Its working fine now. I have made some changes in it. Kindly consider it. I hope it would help.
Scanner myScanner = new Scanner(System.in);
System.out.println("Please input a positive interger that indicates number of positive intergers ");
int number = myScanner.nextInt();
while (number <= 0) {
System.out.println("Please input interger");
number = myScanner.nextInt();
}
int[] arr = new int[number]; // Store values in array
int i=0; //i is to store current iteration
int sum=0; //sum is to store sum of the input
int x; //x is to store the user input
int max = 0;
int min = 0;
for(i=0;i<number;i++){
System.out.println("Please input a positive interger ");
arr[i] = myScanner.nextInt();
sum = sum + arr[i];
}
int average = sum/number;
System.out.println("The average is " + average);
// Put initial values in min and max for comparing
min =arr[0];
max = arr[0];
for(i=1;i<number;i++)
{
// Compare if values is less than arr[i]
if (arr[i] < min){
min = arr[i];
}
// Compare if values is greater than arr[i]
if (arr[i] > max) {
max = arr[i];
}
}
System.out.print("The minimum value is " + min);
System.out.println( " and the maximum value is " + max);
}
}
Here is the output:
So, I wanted to insert an array of numbers, and return 3 numbers in order biggest to smallest with the same difference between each two.
Example:
2 3 7 9 12.
Return:
2 7 12 because 2+5=7, 7+5=12.
The code below is my attempt. I made 3 for loops.
Loops:
The first went through the entire array, picked a number.
The second: for the remaining numbers, picked a smaller number. Calculate their difference.
The third: finds a third number that is smaller and has that same difference vs. the second number.
So: (first number - second number) = (second number - third number)
public static void main(String[] args) {
int n;
int num1;
int num2;
int num3;
int dif;
Scanner scan = new Scanner(System.in);
System.out.print("How many numbers do you want to choose from? ");
n = scan.nextInt();
int nums[] = new int[n];
System.out.println("Please input the integers: ");
for (int i=0; i<n ;i++){
nums[i] = scan.nextInt();
}
System.out.println(" ");
for (int i=0; i<n; i++){
for (int j=i+1; j<n; j++){ //compare element i to the rest of the array
if(nums[j]<= nums[i]){ //if a num at j is smaller than num at i,
num3 = nums[i]; //then num3 is num at i
num2 = nums[j]; //and num2 is num at j
dif = num3 - num2; //find the difference
for(int k=i+j+1; k<n; k++){
if(num2 == (nums[k]+ dif)){ //if num2 is num at k + difference
num1 = nums[k]; //then num1 must be num at k
}
}
}
}
}
System.out.print(num3); //This is the effort printing them out
System.out.print(num2); //But for some reason I couldn't
System.out.print(num1); //even I initialized num3,2,1 outside of the for loop
scan.close(); //closing the scanner object
}
This works with two fors, if you are looking for faster one.
public static void main(String[] args) throws Exception {
List<Integer> integers = Arrays.asList(1, 3, 5, 9, 17);
Map<String, Integer> differenceMap = new HashMap<>();
for (int i = 0; i < integers.size(); i++) {
int first = integers.get(i);
for (int j = i + 1; j < integers.size(); j++) {
int second = integers.get(j);
int difference = second - first;
int next = difference + second;
if (integers.contains(next)) {
differenceMap.put(first + " - " + second + " - " + next, difference);
}
}
}
differenceMap.keySet().forEach(System.out::println);
}
I need to write a code that takes the users input of numbers and adds them, displays the amount of positives, negatives, zeroes, and the count of the amount of numbers inputted once the user enters the letter 'e'.
When i enter 'e' though, the program terminates without printing the count or average.
Also, the average doesn't work because it says i cannot divide by zero.
public static void main (String[] args){
Scanner input = new Scanner(System.in);
int negative = 0;
int positive = 0;
int zeroes = 0;
int sum = 0;
int count = 1;
int average = sum / (count - 1);
do{
System.out.print("Enter a float or 'e' to exit");
String entered = input.nextLine();
if("e".equals(entered)){
//print stuff
break;
}else{
int num;
try {
num = Integer.parseInt(entered);
} catch (NumberFormatException e) {
System.out.print(negative + positive +
zeroes + sum + (count - 1) + average);
continue; // re-do the loop
}
if(num < 0){
sum += num;
count++;
negative++;
}else if (num > 0){
sum += num;
count++;
positive++;
}else{//similar to comment above
sum += num;
count++;
zeroes++;
}
}
} while(true);
}
}
You are missing the print in "if" condition. Modify the if condition as follows
if("e".equals(entered)){
//print stuff
System.out.print(negative + positive + zeroes + sum + (count - 1) + average);
break;
}
As others people have commented, you need to consider cases like "divide by zero" and also handle input validation.
Here is what I have to do:
"Write a segment of code that reads a sequence of integers from the keyboard until the user enters a negative number. It should then output a count of the number of even integers and the number of odd integers read (not including the final negative value in either count). Remember - 0 is an even number. For example, if the sequence is:
2
7
15
5
88
1243
104
-1
Then the output should be
Number of even integers: 3
Number of odd integers: 4
My code just keeps going even after inputting -1. I have a feeling I am missing a {somewhere or wrote the code wrong. Here is my code:
int oddCount = 0, evenCount = 0;
Scanner in = new Scanner(System.in);
while (oddCount>=0&&evenCount>=0){
System.out.print("Enter an integer: ");
int temp = in.nextInt();
if (temp>0) {
if (temp%2==0)
evenCount = evenCount + 1;
else oddCount = oddCount + 1;
while (temp>0);
System.out.println("Number of even integers: "+evenCount);
System.out.println("Number of odd integers: " +oddCount);
}
}
I think using do While loop will also prevent your isses,
int oddCount = 0;
int evenCount = 0;
Scanner in = new Scanner(System.in);
do {
System.out.print("Enter an integer: ");
int temp = in.nextInt();
if (temp > 0) {
(temp % 2==0)? evenCount++:oddCount++;
}
} while (temp > 0);
Your loop will always continue because while (oddCount>=0&&evenCount>=0) will always be true in your case. Try it like this:
int oddCount = 0, evenCount = 0;
Scanner in = new Scanner(System.in);
boolean continue = true;
while (continue){
System.out.print("Enter an integer: ");
int temp = in.nextInt();
if (temp>0) {
if (temp%2==0)
evenCount++;
else
oddCount++;
}
else {
System.out.println("Number of even integers: "+evenCount);
System.out.println("Number of odd integers: " +oddCount);
continue = false;
}
}
Your code reads just one integer, then stuck at this line: while (temp>0);
The code below solve your problem:
int oddCount = 0, evenCount = 0, temp;
Scanner in = new Scanner(System.in);
System.out.print("Enter an integer: ");
while (true) {
temp = in.nextInt();
if (temp < 0) {
break;
} else if (temp % 2 == 0) {
evenCount = evenCount + 1;
} else {
oddCount = oddCount + 1;
}
}
System.out.println("Number of even integers: " + evenCount);
System.out.println("Number of odd integers: " + oddCount);
There are a few problems here.
First, since you never decrease oddCount and evenCount, the condition oddCount>=0&&evenCount>=0 will always be true.
Second, you have an empty loop that loops endlessly, since it's condition is true and it has no body: while (temp>0);
I'd just take out the first loop, which is redundant, and use a do while loop:
int oddCount = 0;
int evenCount = 0;
Scanner in = new Scanner(System.in);
do {
System.out.print("Enter an integer: ");
int temp = in.nextInt();
if (temp > 0) {
if (temp % 2==0) {
evenCount++;
} else {
oddCount++;
}
}
} while (temp > 0);
System.out.println("Number of even integers: " + evenCount);
System.out.println("Number of odd integers: " + oddCount);
Try this..
Scanner s=new Scanner(System.in);
int evenCount=0, oddCount=0;
while(true)
{
System.out.println("Enter a number");
int n=s.nextInt();
if(n<0) break;
if(n%2==0) evenCount+=n;
else oddCount+=n;
}
System.out.println("even count "+evenCount);
System.out.println("odd count "+oddCount);