import java.util.*; class Tester { public static void main(String[] args) { Scan
ID: 3874374 • Letter: I
Question
import java.util.*;
class Tester {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter four variables under or equal to 255");
System.out.println("First variable:");
String a=sc.next();
System.out.println("Second variable:");
String b=sc.next();
System.out.println("Third variable:");
String c=sc.next();
System.out.println("Fourth variable:");
String d=sc.next();
int num[]={Integer.valueOf(a),Integer.valueOf(b),Integer.valueOf(c),Integer.valueOf(d)};
for(int x=0;x<3;x++){
System.out.println("Switch status for data value "+ num[x]+":");
for(int j=6;j<14;j++){
Integer.toBinaryString(num[x]);
for(int y=8;y>0;y--){
if(num[x].substring(y-1,y)==1){
System.out.print("Switch sw5"+j+"is on");
}else{
System.out.print("Switch sw5"+j+"is off");
}
}
}
}
}
}
I need help with my code for BPJ Java Project Masking Telemetry Data(without creating a new file/created scanner), I need to be able to imput four numbers and return "on" for 1 and "off" for 0 in their binary form. The line of code: "if(num[x].substring(y-1,y)==1){" is the main problem, as I can't think of other form of how to do it or if it's even legal.
Explanation / Answer
Code:
import java.util.*;
class Tester
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int[] numbers = new int[4];
System.out.println("Enter four variables under or equal to 255");
for (int i = 0; i < numbers.length; i++)
{
System.out.println("Please enter number "+(i+1));
numbers[i] = input.nextInt();
if(numbers[i]==0)
{
System.out.println("Switch "+(i+1)+" is off");
}
if(numbers[i]==1)
{
System.out.println("Switch "+(i+1)+" is on");
}
}
}
}
OUTPUT:
Enter four variables under or equal to 255
Please enter number 1
0
Switch 1 is off
Please enter number 2
1
Switch 2 is on
Please enter number 3
0
Switch 3 is off
Please enter number 4
1
Switch 4 is on
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.