I'm new to coding, taking a online course at the moment. Now I'm stuck, and I cant find anything to help min move on. The task is to randomly print the amount of number given to the program. Input 5, and the program gives you 5 random numbers. After that sorting them in odd and even. This is all fine.
My problem is that I don't know how to count the numbers in each array (not adding them together) but counting how many odd numbers there is and how any even numbers there is.
Looking for help and guidance.
P.s sorry if there is an answer out there, did my best to find it before asking. d.s
import java.util.*;
public class RandomNbrs {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("How many random numbers do you want? (0-999)");
int numb = scan.nextInt();
System.out.println("Your random numbers:");
int array[] = new int[numb];
for(int i =0; i < numb; i++){
array [i] = (int) (0 + 1000 * Math.random());
System.out.print(array[i]);
System.out.print(" ");
}
System.out.println();
System.out.println();
System.out.println("Even numbers: ");
for(int j =0; j < numb; j++){
if(array[j] %2 == 0){
System.out.print(array[j]);
System.out.print(" ");
}
}
System.out.println();
System.out.println();
System.out.println("Odd numbers: ");
int oddNbr = 0;
for(int k =0; k < numb; k++){
if(array[k] %2 == 1){
System.out.print(array[k]);
System.out.print(" ");
}
}
}
}
There are a few concepts that you've probably misunderstood.
You say you need to create two different array for odd and even numbers. However, in the code that you have provided I only see 1 array called 'array'. Note that array[j] and array[k] refer to the same array. j and k are variables.
So, in case you really need two different array for the two numbers, you need to create two more arrays.
There are several ways, but this would be the simplest (might not necessarily be the best though) going by your code:
import java.util.*;
public class RandomNbrs
{public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
System.out.println("How many random numbers do you want? (0-999)");
int numb = scan.nextInt();
System.out.println("Your random numbers:");
int array[] = new int[numb];
int temp,countOdd=0,countEven=0;
for(int i =0; i < numb; i++){
//I am storing the random generated number in a temporary variable
//Then i check if it is odd or even and increase the count by using a counter
temp= (int) (0 + 1000 * Math.random());
if (temp%2 == 0):
countEven++;
else
countOdd++;
array[i]=temp
System.out.print(array[i]);
System.out.print(" ");
}
System.out.println();
System.out.println();
//Here i am creating two new arrays as you specified.
//if you just want to print the odd and even numbers AND their couns, you dont necessarily need to make these two arrays.
int arrayOdd[] = new int[countOdd];
int arrayEven[]=new int[countEven];
//Counter for accessing the elements of the two new arrays
int counter1=0,counter2=0;
//Filling the two new arrays with odd and even numbers from the prev array
for(int j =0; j < numb; j++){
if(array[j] %2 == 0){
arrayEven[counter1++]=array[j];
}
else
arrayOdd[counter2++]=array[j];
}
//Print odd
System.out.println("Odd:");
for(int k =0; k < countOdd; k++){
System.out.print(arrayOdd[k]);
System.out.print(" ");
}
System.out.println("Total odd numbers="+countOdd);
//or arrayOdd.length() also gives the count.
//Similar for printing even numbers
}}
Additionally you can also use ArrayList where you can dynamically and directly store odd and even generated random numbers in 2 arrays and you do not need the third common array. Let me know if you'd like to know how to this one. :)
this is how I solved the task:
public class testTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
int oddCounter=0;
int evenCounter=0;
Scanner scan = new Scanner(System.in);
System.out.println("How many random numbers do you want? (0-999)");
int numb = scan.nextInt();
System.out.println("Your random numbers:");
int array[] = new int[numb];
for(int i =0; i < numb; i++){
array [i] = (int) (0 + 1000 * Math.random());
System.out.print(array[i]);
System.out.print(" ");
}
System.out.println();
System.out.println();
for(int j =0; j < numb; j++){
if(array[j] %2 == 0){
evenCounter++;
}
}
System.out.println("Dessa " + evenCounter + " Even numbers: ");
for(int j =0; j < numb; j++){
if(array[j] %2 == 0){
System.out.print(array[j]);
System.out.print(" ");
}
}
System.out.println();
System.out.println();
for(int k =0; k < numb; k++){
if(array[k] %2 == 1){
oddCounter++;
}
}
System.out.println("Dessa " + oddCounter + " Odd numbers: ");
for(int k =0; k < numb; k++){
if(array[k] %2 == 1){
System.out.print(array[k]);
System.out.print(" ");
}
}
}
}
Related
I've been looking for different solutions out here but on my code they won't work. I'm getting an input from a user but then it doesn't display what's inside the if-else statement. I tried to do a different code for the if statement but it doesn't work. The int input works but it doesn't work in the string input.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int numbers [] = new int[10];
int temp;
String sc = " ";
Scanner scan = new Scanner(System.in);
// Loop through array
// Change value of each array per iteration
System.out.print("Enter 10 integers: ");
for(int i = 0; i < numbers.length; i++){
numbers[i] = scan.nextInt();
}
System.out.print("\nPlease Choose Among The Following:");
System.out.println("\n A - Display the numbers \t B - Display the values of even indexes \n C - Display the values of odd indexes \t D - Display the values in ascending order \n E - Display the values in descending order");
System.out.print("\nEnter The Letter of Your Choice: ");
sc = scan.nextLine();
if (sc.equals('A') || sc.equals('a')){
System.out.print("Display each item in array");
for(int number: numbers){
System.out.println(number);
}}
else if (sc.equals('B') || sc.equals('b')){
System.out.println("Display odd indexes");
for(int i = 0; i < numbers.length; i++){
if(i % 2 == 0){
System.out.println(numbers[i]);
}
}}
else if (sc.equals('C') || sc.equals('c')){
System.out.println("Display even indexes");
for(int i = 0; i < numbers.length; i++){
if(i % 2 == 1){
System.out.println(numbers[i]);
}
}}
else if (sc.equals('D') || sc.equals('d')){
int [] ascendingList = numbers.clone();
System.out.println("Display in ascending order using bubble sort");
for(int i = 0; i < ascendingList.length - 1 ; i++){
for(int j = 0; j < (ascendingList.length - i - 1); j++){
if(ascendingList[j] > ascendingList[j+1]){
temp = ascendingList[j];
ascendingList[j] = ascendingList[j+1];
ascendingList[j+1] = temp;
}
}
}
for(int number: ascendingList){
System.out.println(number);
}}
else if (sc.equals('E') || sc.equals('e')){
int [] descendingList = numbers.clone();
System.out.println("Display in descending order using bubble sort");
for(int i = 0; i < descendingList.length - 1 ; i++){
for(int j = 0; j < (descendingList.length - i - 1); j++){
if(descendingList[j] < descendingList[j+1]){
temp = descendingList[j];
descendingList[j] = descendingList[j+1];
descendingList[j+1] = temp;
}
}
}
for(int number: descendingList){
System.out.println(number);
}}
}
}
Im trying to create a descending triangle of numbers like this in Java:
4
3 3
2 2 2
1 1 1 1
The user inputs the first number and then it's supposed to create the descending triangle and honestly I don't know how to figure it out.
I haven't really tried that much I'm just lost :)
Here's my code so far:
public static void numberlines(){
Scanner in = new Scanner(System.in);
System.out.println("Welcome to number lines, enter a number and I'll give you some other numbers in a line...");
int usernum;
int counter = 0;
int count = 0;
char tab = 9;
int i;
usernum = getInt();
while (counter != usernum){
if (usernum > counter) {
usernum--;
System.out.println(+ usernum);
}else if (usernum < counter){
usernum++;
System.out.println(+usernum);
}
}//while
}//numberlines
Right now it just prints the descending line of numbers but I'm pretty sure there's a lot more to it. If anyone has any suggestions or ideas that would be awesome. Thanks
You can do this by nested while loops:
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Welcome to number lines, enter a number and I'll give you some other numbers in a line...");
int usernum;
int i = 1;
usernum = in.nextInt();
while(i <= usernum){
int j = 1;
while(j<=i){
System.out.print(usernum-i+1);
j++;
}
System.out.println();
i++;
}//while
}//numberlines
You can use this:
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int usernum = s.nextInt();
for(int i = 1; i <= usernum; i++){
for(int j = 1; j <= i; j++){
System.out.print(usernum-i+1);
}
System.out.println();
}
}
The question I am working on is:
A square with the side length n is filled with numbers 1,2,3,...n2 is a magic square if the sum of elements in each row, column, and two diagonals is the same value.
Write a program that reads in a value of 16 values from the keyboard and tests whether they form a magic square when put into a 4 x 4 array. You need to test the following 2 features:
Does each number of 1,2,...16 occur in user input? Tell the user to try again if they enter a number that they've already entered.
When the numbers are put into a square, are the sums of rows, columns, and diagonals equal to each other?
It must be done using two-dimensional array
I am having trouble with asking the user to try again if they enter a number that they have previously entered. And, numbers in 4 x 4 do not print.
What am I doing wrong? How can I fix it?
This is the code I have so far:
Scanner in = new Scanner (System.in);
int n =4;
int[][] square = new int[n][n];
int number = 0;
int num = 0;
for (int i = 0; i <16; i++){
number = num;
System.out.print ("Enter a number: ");
num = in.nextInt();
int num_2 = 0;
if (number==num || number==num_2) {
System.out.println ("Try again.");
System.out.println ("Enter a number: ");
num_2 = in.nextInt();
}
if (num > 16){
System.out.println ("Try again.");
break;
}
}
for (int i= 0; i < n; i++){
for (int j = 0; j < n; j++) {
num+=square [i][j];
System.out.print(square[i][j] + "\t");
}
}
}
}
You can try this code for 1st feature and add code for 2nd one.
int ar[][] = new int[4][4];
System.out.println("Enter Numbers");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
boolean flag = false;
int num = sc.nextInt();
if (num > 16 || num < 1) {
System.out.println("Please Enter number between 1 to 16");
flag=true;
j--;
} else {
for (int k = 0; k <= i; k++) {
for (int l = 0; l <= j; l++) {
if (ar[k][l] == num) {
System.out.println("This number already inserted...Please give another");
j--;
flag = true;
}
}
}
}
if (!flag) {
ar[i][j] = num;
}
}
}
If you can't understand anything please ask.
Hope this help.
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'm trying to print all from 6 to n but when i run the code it gives me all the numbers but not the right perfect numbers. For example when I enter 30 it prints out all numbers but it says only 6 and 7 are perfect numbers only 7 is not a perfect number and 28 is.
import java.util.Scanner;
public class PerfectN {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number that is 6 or above: ");
int n = scanner.nextInt();
int sum = 0;
if(n<6){
System.out.println("Incorrect number.");
}
else
for(int i = 6;i<=n;i++){
for(int j = 1; j<i; j++){
if(i%j==0)
sum += j;
}
if(sum==i){
System.out.println(i + " is a perfect number.");
}
else
System.out.println(i + " is not a perfect number.");
}
}
}
You need to reset the sum variable to zero for each number you go through:
for (int i = 6; i <= n; i++) {
int sum = 0;
for (int j = 1; j < i; j++) {