my whole code works fine, except for the common occurrences part. If you input 1 number the most, it will work, but if you enter 2 numbers an equal amount of times, it won't work anymore. Any additional advice to improve my code would be much appreciated.
public static void Thingy() {
Scanner input = new Scanner(System.in);
int min = 999;
int max = 0;
int range;
int prevocc = 0;
int occ = 0;
int common = 0;
int enter = 1;
int a1_5 = 0;
int a6_10 = 0;
int a11_15 = 0;
int a16_20 = 0;
int a21_25 = 0;
int a26_30 = 0;
int a31_35 = 0;
int a36_40 = 0;
int a41_45 = 0;
int a46_50 = 0;
int[] num;
num = new int[99999];
for (int i = 1; i < 99999 && enter != 0; i++) {
System.out.println("Enter a number between 1 and 50, enter 0 to quit");
num[i] = input.nextInt();
enter = num[i];
if (num[i] > max) {
max = num[i];
}
if (num[i] < min) {
if (num[i] != 0) {
min = num[i];
}
}
if (num[i] > 50) {
System.out.println("Your number is too high, enter another number");
num[i] = input.nextInt();
}
if (num[i] < 0) {
System.out.println("Your number is too low, enter another number");
num[i] = input.nextInt();
}
for (; num[i] > 0; num[i]--) {
if (enter == num[i]) {
occ++;
}
if (occ > prevocc) {
common = num[i];
}
prevocc = occ;
}
num[i] = enter;
}
for (int j = 50; j > 0; j--) {
if (num[j] >= 1 && num[j] <= 5) {
a1_5++;
} else if (num[j] >= 6 && num[j] <= 10) {
a6_10++;
} else if (num[j] >= 11 && num[j] <= 15) {
a11_15++;
} else if (num[j] >= 16 && num[j] <= 20) {
a16_20++;
} else if (num[j] >= 21 && num[j] <= 25) {
a21_25++;
} else if (num[j] >= 26 && num[j] <= 30) {
a26_30++;
} else if (num[j] >= 31 && num[j] <= 35) {
a31_35++;
} else if (num[j] >= 36 && num[j] <= 40) {
a36_40++;
} else if (num[j] >= 41 && num[j] <= 45) {
a41_45++;
} else if (num[j] >= 46 && num[j] <= 50) {
a46_50++;
}
}
System.out.print("1 - 5: ");
while (a1_5 > 0) {
System.out.print("*");
a1_5--;
}
System.out.println("");
System.out.print("6 - 10: ");
while (a6_10 > 0) {
System.out.print("*");
a6_10--;
}
System.out.println("");
System.out.print("11 - 15: ");
while (a11_15 > 0) {
System.out.print("*");
a11_15--;
}
System.out.println("");
System.out.print("16 - 20: ");
while (a16_20 > 0) {
System.out.print("*");
a16_20--;
}
System.out.println("");
System.out.print("21 - 25: ");
while (a21_25 > 0) {
System.out.print("*");
a21_25--;
}
System.out.println("");
System.out.print("26 - 30: ");
while (a26_30 > 0) {
System.out.print("*");
a26_30--;
}
System.out.println("");
System.out.print("31 - 35: ");
while (a31_35 > 0) {
System.out.print("*");
a31_35--;
}
System.out.println("");
System.out.print("36 - 40: ");
while (a36_40 > 0) {
System.out.print("*");
a36_40--;
}
System.out.println("");
System.out.print("41 - 45: ");
while (a41_45 > 0) {
System.out.print("*");
a41_45--;
}
System.out.println("");
System.out.print("46 - 50: ");
while (a46_50 > 0) {
System.out.print("*");
a46_50--;
}
System.out.println("");
System.out.println("The highest number is: " + max);
System.out.println("The lowest number is " + min);
System.out.println("The most commonly occuring number is: " + common);
System.out.println("The range is: " + (max - min));
}
Related
I've written this piece of code to simulate a galton box for a school project:
'''
public class GaltonBoxProject {
public static void main(String[]args){
//This will keep track of the final positions and will sort them
int[] finalColumns = {0,0,0,0,0,0,0};
//will track current pos of the ball
//8 tiers
//start from middle of the box
int randChance;
for(int a = 0; a < 100; a++) {
int currentPos = 3;
for (int i = 0; i < 9; i++) {
//resets the number
randChance = (int) (Math.random() * 100) + 1;
//if less than 50 go to the left
//if 0 --> +
//if 8 --> -
if(currentPos == 0 && randChance > 50) {
currentPos++;
}
else if (currentPos == 7 && randChance <= 50) {
currentPos--;
}
else if(currentPos != 7 && currentPos != 0 && randChance > 50){
currentPos++;
}
else if(currentPos != 7 && currentPos != 0 && randChance < 50){
currentPos--;
}
//If you want to know why I got these results
//I genuinely can't find the reason why this pattern happens
//System.out.print(" RAND: " + randChance);
//System.out.println(" POS: " + currentPos);
}
//Also debugging
//System.out.println(a + " CURRENT POS: " + currentPos);
//This will sort the numbers
for(int k = 0; k < 7; k++){
if(k == currentPos){
finalColumns[k]++;
}
}
}
//for(int i = 0; i < 8; i ++) {
// System.out.println("KEY: " + finalColumns[i]);
}
for(int k = 0; k < 7; k++){
System.out.print(k + ": ");
for(int j = 0; j < finalColumns[k]; j++){
System.out.print("*");
}
System.out.println("");
}
}
'''
I get this result:
0: ********
1: ********
2: **********************
3: ***
4: ******************************
5: *****
6: *****************
I should be getting a bell curve and I don't know why the code is doing this
That's the multi dimensional array :
{ 3 13 15 28 30} ,
{55 54 53 27 26} ,
{54 12 52 51 50} ,
{50 10 8 53 11} ,
The output should be if num = 1 : 6 , because from [1][0] to [1][2] its 3 and then from [2][2] to [2][3] its 2 so it will be 6 , num its the slope that the user want , the slope can be to the right or to down , its forbidden to use loop of any kind , only recursion, tried for like 8 hours to solve it but i am so confused , here is my code :
private static int longestSlope (int [][] mat, int num , int i , int j , int count , int temp,int oldi,int oldj,boolean found)
{
System.out.println("oldi " +oldi);
System.out.println("oldj " +oldj);
System.out.println("temp " +temp);
System.out.println("count "+count);
System.out.println("i "+i);
System.out.println("j "+j);
if(i == mat.length-1 && j == mat[0].length-1)
return count;
if(i != mat.length && j != mat[0].length)
{
if(j < mat[0].length-1 && mat[i][j] - num == mat[i][j+1] ) // keep j+
{
if(temp == 0)
{
found = true;
oldj = j;
oldi = i;
}
System.out.println("check2");
if(j == 0)
temp = longestSlope(mat,num,i,j+1,count,temp+1,oldi,oldj,found);
else
temp = longestSlope(mat,num,i,j+1,count,temp+1,oldi,oldj+1,found);
}
else if(i < mat.length-1 && mat[i][j] - num == mat[i+1][j])
{
if(temp == 0)
{
found = true;
oldj = j;
oldi = i;
}
temp = longestSlope(mat,num,i+1,j,count,temp+1,oldi,oldj,found);
}
else if(j < mat[0].length-1)
temp = longestSlope(mat,num,i,j+1,count,temp,oldi,oldj,found);
else if(found)
{
System.out.println(found);
System.out.println("found i "+i);
System.out.println("found j "+j);
found = false;
if(j != mat[0].length-1)
temp = longestSlope(mat,num,oldi,oldj,count,0,oldi,oldj,found);
else if(j == mat[0].length-1 && temp != 0)
temp = longestSlope(mat,num,oldi,oldj,count,0,oldi,oldj,found);
else
temp = longestSlope(mat,num,oldi+1,0,count,0,oldi,oldj,found);
}
}
if(i < mat.length-1 && !found)
{
System.out.println("oldj222222222 "+oldj);
return longestSlope(mat,num,i+1,0,count,0,0,0,found);
}
return 0;
}
I have an assignment where my prof. will input an unspecified number of integers into my program, and my program will process the data accordingly to assign a grade to each value and find the highest and lowest value, however he always places a -1 at the end of the data so that when input<0 it will stop prompting for input.
The problem is my program isn't supposed to process the -1. It doesn't get added as a grade, but it is a part of the list of grades, so the lowest value is always -1.
I've tried writing code that would remove the -1 from my array when detected as a part of the logic to evaluate a grade, but everything I've tried only returns errors.
How can I ignore the -1 and find the next lowest value, or remove the -1 from the list before evaluating?
import java.util.*;
public class Hw5{
public static void main(String[] args){
Scanner console = new Scanner(System.in);
int input = console.nextInt();
ArrayList<Integer> GradeList = new ArrayList<>();
while(input >= 0){
input = console.nextInt();
GradeList.add(input);
}
evaluateGrade(GradeList);
System.out.print(GradeList);
}
public static void evaluateGrade(List<Integer> Scores){
int count = Scores.size();
int high = Collections.max(Scores);
int low = Collections.min(Scores);
int Aplus = 0;
int A = 0;
int Amin = 0;
int Bplus = 0;
int B = 0;
int Bmin = 0;
int Cplus = 0;
int C = 0;
int Cmin = 0;
int Dplus = 0;
int D = 0;
int Dmin = 0;
int F = 0;
for(int i : Scores){
if(i > 97){
Aplus += 1;
}
else if(i >= 93 && i <= 96){
A += 1;
}
else if(i >= 89 && i <= 92){
Amin += 1;
}
else if(i >= 85 && i <= 88){
Bplus += 1;
}
else if(i >= 81 && i <= 84){
B += 1;
}
else if(i >= 77 && i <= 80){
Bmin += 1;
}
else if(i >= 81 && i <= 84){
B += 1;
}
else if(i >= 77 && i <= 80){
Bmin += 1;
}
else if(i >= 76 && i <= 73){
Cplus += 1;
}
else if(i >= 69 && i <= 72){
C += 1;
}
else if(i >= 65 && i <= 68){
Cmin += 1;
}
else if(i >= 61 && i <= 64){
Dplus += 1;
}
else if(i >= 57 && i <= 60){
D += 1;
}
else if(i >= 53 && i <= 56){
Dmin += 1;
}
else if(i >= 52 && i <= 0){
F += 1;
}
}
System.out.printf("Total number of grades = %d", count);
System.out.println();
System.out.printf("Number of A+'s = %d", Aplus);
System.out.println();
System.out.printf("Number of A's = %d", A);
System.out.println();
System.out.printf("Number of A-'s = %d", Amin);
System.out.println();
System.out.printf("Number of B+'s = %d", Bplus);
System.out.println();
System.out.printf("Number of B's = %d", B);
System.out.println();
System.out.printf("Number of B-'s = %d", Bmin);
System.out.println();
System.out.printf("Number of C+'s = %d", Cplus);
System.out.println();
System.out.printf("Number of C's = %d", C);
System.out.println();
System.out.printf("Number of C-'s = %d", Cmin);
System.out.println();
System.out.printf("Number of D+'s = %d", Dplus);
System.out.println();
System.out.printf("Number of D's = %d", D);
System.out.println();
System.out.printf("Number of D+'s = %d", Dmin);
System.out.println();
System.out.printf("Number of F's = %d", F);
System.out.println();
System.out.printf("The highest grade = %d", high);
System.out.println();
System.out.printf("The lowest grade = %d", low);
System.out.println();
}
Change this
int input = console.nextInt();
ArrayList<Integer> GradeList = new ArrayList<>();
while(input >= 0){
input = console.nextInt();
GradeList.add(input);
}
To something like
List<Integer> gradeList = new ArrayList<>();
int input;
while ((input = console.nextInt()) >= 0) {
gradeList.add(input);
}
Which programs to the List interface, follows the Java idiomatic convention of assignment and evaluation and observes standard variable naming conventions.
"Only one while loop should be used to determine all even and odd numbers between 50 and 100."
public class EvenOdd {
public static void main(String args[]) {
int x = 50;
int y = 50;
int i = 0;
int j = 0;
int n = 0;
System.out.print("Even numbers between 50 and 100: ");
while((i != x) || (j != y)) {
n++;
System.out.print(i + x + ", ");
i += 2;
if(i != x)
continue;
System.out.println("100");
System.out.print("\nOdd numbers between 50 and 100: ");
System.out.print((j+1) + y + ", ");
j += 2;
if(j != y)
continue;
}
}
}
The evens print fine but the odds continue on forever. This may be a dumb question, but I'm having the biggest brainfart right now, and I would really appreciate help on this.
Simply try this and let me know. It's based on your code, with just a few minor adjustments:
public class Teste4 {
public static void main(String args[]) {
int x = 50;
int y = 50;
int i = 0;
int j = 1;
int n = 0;
System.out.print("Even numbers between 50 and 100: ");
while((i < x) || (j < y)) {
if(i < x){
System.out.print(i + x + ", ");
i += 2;
continue;
}else if(i == x){
System.out.println("100");
i++;
}
if(j == 1){
System.out.print("\nOdd numbers between 50 and 100: ");
}
System.out.print(j + y + ", ");
j += 2;
}
}
}
Perhaps you should try this
int even_counter = 50;
int odd_counter=51;
System.out.println("Even numbers between 50 and 100: ");
while((even_counter < 100 ) || (odd_counter < 100)){
if(even_counter < 100){
System.out.print(even_counter+ " ");
even_counter+=2;
continue;
}
if(odd_counter < 100){
if(odd_counter == 51){
System.out.println("\nOdd numbers between 50 and 100: ");
}
System.out.print(odd_counter+ " ");
odd_counter+=2;
}
}
I'm sure this is an assignment but i'll add my 2 cents since it's solved. This one will give you an array.
final int EVEN = 0, ODD = 1;
int low = 50, high = 100, current = low;
int[][] numbers = new int[2][];
numbers[EVEN] = new int[((high - low) / 2) + ((high - low) % 2)];
numbers[ODD] = new int[((high - low) / 2)];
while(current < high){
numbers[current % 2][(current - low) / 2] = current++;
}
System.out.println("EVEN" + Arrays.toString(numbers[EVEN]));
System.out.println("ODD " + Arrays.toString(numbers[ODD]));
Alternative approach ,
int current = 50 , end = 100 ;
String odd , even ;
while(current <= 100){
if (current % 2 == 0)
even = even.concat ("," + current);
else
odd = odd.concat("," + current);
current++;
}
System.out.println("Even no are : " + even);
System.out.println("Odd no are : " + odd);
I dont have a compiler now . I think this should be right :) .
Basically, the user enters student grades and the program will display and arrange them into sections (0-29 marks, 30-39 marks, 40-69 marks and 70-100 marks). If the user enters anything higher than 100 then the program terminates and outputs the results.
I'm just having trouble printing out the output vertically.
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Type in the students marks. Type above 100 to show output.");
int marks;
int starsfrom0to29 = 0;
int starsfrom30to39 = 0;
int starsfrom40to69 = 0;
int starsfrom70to100 = 0;
int counter = 0;
marks = input.nextInt();
while (marks < 100) {
//PUTTING MARKS INTO CATEGOERY
if ((marks >= 0) && (marks <= 29)) {
starsfrom0to29++;
}
if ((marks >= 30) && (marks <= 39)) {
starsfrom30to39++;
}
if ((marks >= 40) && (marks <= 69)) {
starsfrom40to69++;
}
if ((marks >= 70) && (marks <= 100)) {
starsfrom70to100++;
}
if (marks < 100) {
counter++;
}
marks = input.nextInt();
}
//PRINTING OUT NUMBER OF STARS
System.out.println();
System.out.print("0-29 ");
for (int x = 0; x < starsfrom0to29; x++) {
System.out.print("*");
}
System.out.println();
System.out.print("30-39 ");
for (int x = 0; x < starsfrom30to39; x++) {
System.out.print("*");
}
System.out.println();
System.out.print("40-69 ");
for (int x = 0; x < starsfrom40to69; x++) {
System.out.print("*");
}
System.out.println();
System.out.print("70-100 ");
for (int x = 0; x < starsfrom70to100; x++) {
System.out.print("*");
}
System.out.println();
System.out.println();
}
This prints it out horizontally. E.g: If I typed in the numbers: 23,65,77,87,101 it would display:
0-29 *
30-39
40-69 *
70-100 **
However, I need to find out how to print the output vertically. Basically the stars (asterisks) should be in a vertical line and are below the headlines.
What you need is a single loop that increments all 4 counters, and prints 1-4 asterisks in the same line in each iteration.
int i = 0;
int j = 0;
int k = 0;
int l = 0;
System.out.println("0-29 30-39 40-69 70-100");
while (i<starsfrom0to29 || j < starsfrom30to39 || k < starsfrom40to69 || l < starsfrom70to100) {
if (i<starsfrom0to29) {
System.out.print(" * ");
i++;
} else {
System.out.print(" ");
}
if (j<starsfrom30to39) {
System.out.print(" * ");
j++;
} else {
System.out.print(" ");
}
if (k<starsfrom40to69) {
System.out.print(" * ");
k++;
} else {
System.out.print(" ");
}
if (l<starsfrom70to100) {
System.out.println(" * ");
l++;
} else {
System.out.println("");
}
}
if this is the type of output you are looking for:
0-29
*
*
30-39
*
*
...
swap your PRINTING OUT NUMBERS OF STARS code with the following:
System.out.println();
System.out.println("0-29 ");
for (int x = 0; x < starsfrom0to29; x++) {
System.out.println("*");
}
System.out.println("30-39 ");
for (int x = 0; x < starsfrom30to39; x++) {
System.out.println("*");
}
System.out.println("40-69 ");
for (int x = 0; x < starsfrom40to69; x++) {
System.out.println("*");
}
System.out.println("70-100 ");
for (int x = 0; x < starsfrom70to100; x++) {
System.out.println("*");
}
If this the output you are looking for:-
Type in the students marks. Type above 100 to show output.
12
30
31
17
60
43
89
97
100
0-29 30-39 40-69 70-100
* * * *
* * * *
Then here is the java code:-
import java.util.*;
public class {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Type in the students marks. Type above 100 to show output.");
int marks;
int starsfrom0to29 = 0;
int starsfrom30to39 = 0;
int starsfrom40to69 = 0;
int starsfrom70to100 = 0;
int counter = 0;
int array[]={0,0,0,0};
int i=0;
marks = input.nextInt();
while (marks < 100) {
//PUTTING MARKS INTO CATEGOERY
if ((marks >= 0) && (marks <= 29)) {
starsfrom0to29++;
}
if ((marks >= 30) && (marks <= 39)) {
starsfrom30to39++;
}
if ((marks >= 40) && (marks <= 69)) {
starsfrom40to69++;
}
if ((marks >= 70) && (marks <= 100)) {
starsfrom70to100++;
}
if (marks < 100) {
counter++;
}
marks = input.nextInt();
}
//PRINTING OUT NUMBER OF STARS
System.out.println();
System.out.print("0-29\t30-39\t40-69\t70-100");
for (int x = 0; x < starsfrom0to29; x++) {
array[0]=array[0]+1;
}
System.out.println();
for (int x = 0; x < starsfrom30to39; x++) {
array[1]=array[1]+1;
}
System.out.println();
for (int x = 0; x < starsfrom40to69; x++) {
array[2]=array[2]+1;
}
System.out.println();
for (int x = 0; x < starsfrom70to100; x++) {
array[3]=array[3]+1;
}
for(i=0;i<4;i++)
{
if(array[0]>0)
{
System.out.print("*\t");
array[0]=array[0]-1;
}
else System.out.print("\t");
if(array[1]>0)
{
System.out.print("*\t");
array[1]=array[1]-1;
}
else System.out.print("\t");
if(array[2]>0)
{
System.out.print("*\t");
array[2]=array[2]-1;
}
else System.out.print("\t");
if(array[3]>0)
{
System.out.print("*\t");
array[3]=array[3]-1;
}
else System.out.print("\t");
System.out.print("\n");
}
}
}
Hope this helps!!
Here is the code. Mind you, there are better ways to handle the whole marks range thing, but I don't know what you are allowed or not allowed to use in your assignment, so I stuck to simple things:
First, you find the maximum number of stars you have. This is in order to know how many rows of stars to print. If you have at most 3 stars, then you'll need to print three lines. If you have 5, then 5, and so on:
// Find max
int max = 0;
if ( starsfrom0to29 > max ) {
max = starsfrom0to29;
}
if ( starsfrom30to39 > max ) {
max = starsfrom30to39;
}
if ( starsfrom40to69 > max ) {
max = starsfrom40to69;
}
if ( starsfrom70to100 > max ) {
max = starsfrom70to100;
}
Now you print the header. The trick here is that each heading has to be the same width. Since the widest is 70-100, which is six characters wide, 7 with the space, then we have to space each header right-align in a 7 character field.
System.out.println( " 0-29 30-39 40-69 70-100");
Now we print the columns. For each range, if there is still a star to be printed at this row (that is, if the number of stars is still greater than the row number), we print the star, right aligned in a 7-character field. If there are no more stars in this column, we just print the spaces.
for ( int i = 1; i <= max; i++ ) {
if ( starsfrom0to29 >= i ) {
System.out.print( " *");
} else {
System.out.print( " ");
}
if ( starsfrom30to39 >= i ) {
System.out.print( " *");
} else {
System.out.print( " ");
}
if ( starsfrom40to69 >= i ) {
System.out.print( " *");
} else {
System.out.print( " ");
}
if ( starsfrom70to100 >= i ) {
System.out.print( " *");
} else {
System.out.print( " ");
}
System.out.println();
}
Note that all of the commands are System.out.print() so they don't print end-of-line and just add to the same line. For this reason, when we are finished with a row we need to use one System.out.println() to give the end-of-line for the whole row.