Help! in Java, test values {0, 1, 3, 6, 10} I need this to print 107 but it keeps printing 75. What am I doing wrong???
public int switchCase( int []values) {
int sum = 0;
for(int i =
0;i<values.length;i++){
sum +=
values[i];
}
int value =
values.length;
switch
(sum){
case 1:
sum += value + 0;
break;
case 2:
case 3:
case 4:
sum += value + 1;
break;
case 5:
case 6:
case 7:
sum += value + value;
break;
default:
sum += value + value*10;
break;
}
return sum;
}
ANSWER: In your code as we know that the sum of the array elements is 20 and in the switch case you are sending sum as switch(sum) therefore case 20 should be there in the switch case but there is no case 20 so it is going in to the default statement where sum+= value+ value*10 and we know value =5 , so sum = 20 + 5+ 5*10 => 75 that's why it is printing the value 75.
And if you want to print 107 then you have to change the logic and you can change by two ways :1st you can add case 20 so that it can go into it and write logic for 107 as I given below:
CODE:
public class Demo {
public static int switchCase( int []values) {
int sum = 0;
for(int i = 0;i<values.length;i++){
sum += values[i];
}
int value = values.length;
switch (sum){
case 1:
sum += value + 0;
break;
case 2:
case 3:
case 4:
sum += value + 1;
break;
case 5:
case 6:
case 7:
sum += value + value;
break;
case 20:
sum+= value+ 82;
break;
default:
sum += value + value*10;
break;
}
return sum;
}
public static void main (String[] args)
{
int [] values = {0, 1, 3, 6,
10};
System.out.println(switchCase(values));
}
}
OUTPUT:
107
or you can change the logic in to the default statement so that you can get 107.
Help! in Java, test values {0, 1, 3, 6, 10} I need this to print 107...
Show step by step evaluation of java code fragment below: int i; int lucky = -1; switch (lucky * lucky){ case 1: i = 7; break; case 0: i = 13; break; default: i = 0; break;}
JAVA I need a switch so that users can input one selection then another. These selections are for a simple calculator that uses random numbers 1. + 2. - 3. * 4./ 5. RNG. This has to be simple this is a beginners class. Here is what I have import java.util.Scanner; import java.util.Random; import java.util.*; public class You_Michael02Basic_Calculator { public static void main(String[] args) { // Generate random numbers int num1,num2; num1 =(int)(Math.random()*100+1);...
I need a java flowchart for the following code: import java.util.*; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.print("Enter the input size: "); int n=sc.nextInt(); int arr[]=new int[n]; System.out.print("Enter the sequence: "); for(int i=0;i<n;i++) arr[i]=sc.nextInt(); if(isConsecutiveFour(arr)) { System.out.print("yes the array contain consecutive number:"); for(int i=0;i<n;i++) System.out.print(arr[i]+" "); ...
Draw a picture of array A, Showing the values stored in it after execution of each code segment below. Use the following data: 0 1 2 3 4 5 6 7 8 9. Please explain in detail the procedure so I can understand whats going on. import java.util.Scanner; public class ProblemFive { public static void main(String[] args) { int [][] A = new int[4][2]; int k; Scanner keyboard = new...
I had a few questions in Computer Science (Java).
I got these questions wrong and I would like the CORRECT answer
and an EXPLANATION PLEASE.
Please use the correct number when giving the answer such as
1a:.
1b:
etc
1. For Each of the following Bits of code enter the final value of the variable names ans. a. int x = 23; int y = 7; Int ans = x % y; My answer 1.61 (Wrong It was 2) b....
Draw a picture of array A, showing the values stored in it after execution of each code segment below. Use the following data: 0 1 2 3 4 5 6 7 8 9 int A[4][2]: int k, m: for (int i = 0: i < = 3: i++) for (int j = 0: j < = 1: j++) A[i][j] = 0: for (int j = 1: j < = 10: j++) { cin > > k: switch (k) { case...
I need little help with C language. I need to pass what I get from HexToBin(char* hexdec) into char* input so that what I got there it should pass it as string array parametr. Example: Enter IEEE-Hex: 40200000 Equivalent Binary value is : 01000000001000000000000000000000 Decimal Number: 2.5 #include <stdio.h> void HexToBin(char* hexdec) { long int i = 0; while (hexdec[i]) { switch (hexdec[i]) { case '0': printf("0000"); break;...
Hi everyone! I need help on my Java assignment. I need to create a method that is called sumIt that will sum two values and will return the value.It should also invoke cubeIt from the sum of the two values. I need to change the currecnt program that I have to make it input two values from the console. The method has to be called sumIt and will return to the sum that will produce the cube of the sum...
I am using Java to try and solve this problem. Suggestions on
where I am going wrong?
PREVNEXT Workbench ? a Exercise 20662- WORK AREA RESULTS Write the definition of a method named sumArray that has one parameter, an array of ints. The method returns the sum of the elements of the array as an int. x9 of 9: 2018-07-10 12:58:15 -W SUBMIT 1 public int sumArray(int[] a) 2 int i; int sum = 0; for (í 0; í?a.length; ?++)...
Debug the following java code so that it will have given out put at the bottom. Make sure to fix all syntax and logical errors. import java.util.Scanner; public class Graphs { // draws 5 histograms public void drawHistograms() Scanner input = new Scanner( System.in ); int number1 = 0; // first number int number2 = 0; // second number int number3 = 0; // third number int number4 = 0; // fourth number int number5 = 0; // fifth number...