//my java code so far: import java.util.*; public class HW1 { public static void
ID: 3623189 • Letter: #
Question
//my java code so far:import java.util.*;
public class HW1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a sequence of integer values: ");
int data = input.nextInt(); //user enters sequence of integers
int digits = 0;
//data can't be negative, ensuring that only positive integers are entered
while(data > 0) {
if(data % 2 == 0) {
//even
if( data % 2 == 0)
System.out.print(" " + data + " has all even digits.");
}
else if(data % 2 == 1) {
//odd
System.out.print(" " + data + " has all odd digits.");
}
else{
//mix
System.out.print(" " + data + " has a mix of even and odd digits.");
}
digits = data % 10; //extract the first digit
data /= 10; //remove the extracted digit
data = input.nextInt();
}
System.out.println(" End of data found.");
}
Explanation / Answer
You're not looping through all the digits.
import java.util.*;
public class HW1
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a sequence of integer values: ");
int data;
int digits = 0;
//data can't be negative, ensuring that only positive integers are entered
while(input.hasNextInt())
{
data = input.nextInt(); // input next int
boolean even = true;
boolean odd = true;
int temp = data; // temp will be destroyed
while(temp > 0)
{
digits = temp % 10; //extract the first digit
temp /= 10; //remove the extracted digit
if(digits % 2 == 0)
{
odd = false;
}
else
{
even = false;
}
}
if(even)
{
System.out.print(" " + data + " has all even digits.");
}
else if(odd)
{
//odd
System.out.print(" " + data + " has all odd digits.");
}
else
{
//mix
System.out.print(" " + data + " has a mix of even and odd digits.");
}
}
System.out.println(" End of data found.");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.