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);
Related
This program asks for 10 integer numbers and stores these integer numbers in an array.
The program then counts how many odd, how many even, how many negative, how many positive, and how many zero numbers. The program should also display what are the numbers that belong in these groups.
this is my program:
import java.util.Scanner;
public class count {
public static void main (String[] args)
{
int i,x,a[] = new int[10];
int n=0,p=0,e=0,o=0,z=0;
Scanner input = new Scanner(System.in);
System.out.println("Enter 10 Numbers");
for(i=0;i<10;i++)
{
System.out.println("Enter Number :");
a[i] = input.nextInt();
}
for(i=0;i<10;i++)
{
if(a[i]<0)
n++;
else
p++;
x=a[i]%2;
if(x==0)
e++;
else
o++;
if(a[i]==0)
z++;
}
//print
System.out.println("Total Even Numbers = "+e);
System.out.println("Even Numbers are = ");
System.out.println("Total Odd Numbers = "+o);
System.out.println("Odd Numbers are = ");
System.out.println("Total Negative Numbers = "+n);
System.out.println("Negative Numbers are = ");
System.out.println("Total Positive Numbers = "+p);
System.out.println("Positive Numbers are = ");
System.out.println("Total Zero Numbers = "+z);
}
}
This is the output of it.
Enter 10 Numbers
Enter Number :
0
Enter Number :
0
Enter Number :
2
Enter Number :
-3
Enter Number :
-4
Enter Number :
6
Enter Number :
9
Enter Number :
5
Enter Number :
0
Enter Number :
9
Total Even Numbers = 6
Even Numbers are =
Total Odd Numbers = 4
Odd Numbers are =
Total Negative Numbers = 2
Negative Numbers are =
Total Positive Numbers = 8
Positive Numbers are =
Total Zero Numbers = 3
As I said in the comments a simple solution will be to save every number to the corresponding List
List<Integer> evenNumbers = new ArrayList<>();
Then every time you check each number with the if statements instead of having a counter just add the number to the list
if (numbers[i] % 2 == 0) evenNumbers.add(numbers[i]);
And last print them as you already do but check the size of each List
System.out.println("Total Even Numbers = " + evenNumbers.size());
System.out.println("Even Numbers are = " + evenNumbers);
import java.util.Scanner;
public class countPZN {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = 0;
int p = 0, o=0;
int z = 0, e=0;
int num [] = new int[7];
System.out.println("Enter the number");
for (int i = 0; i < 7; i++) {
System.out.println("Enter the number " + (i + 1));
num[i] = input.nextInt();
}
for (int i = 0; i <7; i++) {
if (num[i] < 0)
n++;
else if(num[i]>0)
p++;
else if(num[i]==0)
z++;
else if(num[i]%2==0)
e++;
else
o++;
}
System.out.println("Number of negative number is " + n);
System.out.println("Number of positive number is " + p);
System.out.println("number of zero number is " + n);
System.out.println("number of even number is " + e );
System.out.println("number of odd number is " + o );
}
}
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);
}
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.
I am writing a program that asks for 5 user inputed numbers; however, I need to keep those numbers confined between 1 and 30. I am having trouble writing the while loop for this part of the program.
Here is my code:
Scanner input = new Scanner(System.in);
int numberCounter = 0; // Set scanner to 0
// Intialize numbers 1-5
int number1;
int number2;
int number3;
int number4;
int number5;
String asterisk = "*"; // Holds the string value "*"
int number = 0;
while (number < 1 || number > 30){
System.out.print("Enter the first number (between 1-30): ");
number = keyin.nextInt();
}
System.out.print("Please enter a number between 1 and 30: ");// Calls for user input
number1 = input.nextInt();// Stores user input for number 1
System.out.print("Please enter a number between 1 and 30: ");// Calls for user input
number2 = input.nextInt();// Stores user input for number 2
System.out.print("Please enter a number between 1 and 30: ");// Calls for user input
number3 = input.nextInt();// Stores user input for number 3
System.out.print("Please enter a number between 1 and 30: ");// Calls for user input
number4 = input.nextInt();// Stores user input for number 4
System.out.print("Please enter a number between 1 and 30: ");// Calls for user input
number5 = input.nextInt();// Stores user input for number 5
System.out.printf("%d ", number1);
for (int i = 1; i <= number1; i++)
System.out.print("*");
System.out.println();
System.out.printf("%d ", number2);
for (int i = 1; i <= number2; i++)
System.out.print("*");
System.out.println();
System.out.printf("%d ", number3);
for (int i = 1; i <= number3; i++)
System.out.print("*");
System.out.println();
System.out.printf("%d ", number4);
for (int i = 1; i <= number4; i++)
System.out.print("*");
System.out.println();
System.out.printf("%d ", number5);
for (int i = 1; i <= number5; i++)
System.out.print("*");
System.out.println();
}
}
Can anyone help steer my in the right direction.
Thank you.
You can avoid redundant code by keeping the nextInt() inside while loop and use a counter to track the desired number of inputs you want to read. And exit the loop when all the valid numbers are read.
Scanner input = new Scanner(System.in);
byte maxNum = 31;
byte minNum = 1;
int[] numbers = new int[5];
int number = 0;
int i = 0;
while (i<5) {
System.out.print("Enter number("+(i+1)+") between "+minNum + " and "+maxNum+ " : ");
number = input.nextInt();
if(number >=minNum && number <= maxNum){
numbers[i] = number;
i++;
}else{
System.out.println("--------Invalid Number :"+number+ " ");
}
}
System.out.print("Entered Numbers :");
for(int n : numbers){
System.out.print(n + ",");
}
Note : Code may fail, for char and out of range of inputs.
Try this:
if(Math.max(new int[]{number1,number2,number3,number4,number5}) <= 30 && Math.min(new int[]{number1,number2,number3,number4,number5}) >= 1){
...WITHIN YOUR RANGE...
}else{
}
What this does is check if the max of the set is less than or equal to 30, and the min of the set is more than or equal to 1, and then executing the code.
If your purpose of this program is to ask user to input 5 numbers between 1 and 30, I think you should write 'while' loop for each of the input task to be sure that each number is within the range and to ask user again if it's not.