+ 1
Lottery problem
I wanted a program that has two given integers. If all the 3digits of a given number is the same as the second inputted number, then print "Win"; else, print "Nah". Ex1: Input: 222 2 Output: Win Ex2: Input: 515 5 Output: Nah Ex3: Input: 888 8 Output: Win I need help. I absolutely have no idea how to do this. I did a little coding, but it only chooses the most occurring number: https://code.sololearn.com/cA100a6A15a2
5 ответов
+ 2
Input should see as follows:
7
777
https://code.sololearn.com/c5KtsGlNbvbr/?ref=app
+ 1
Brianna
You are doing logically correct but taking number in reverse. First is 3 digit number next is single digit number as you specified in description...
Next compare it if result is 3 or not. If result==3 ,print Win else print Nah....
you can simply this with (digit*10+digit)*10+digit == integer ? "Win" :"Nah"
+ 1
Good job, Brianna. You just lacked control over result. Now write better. No m, no result, a boolean and less code Something as
import java.util.Scanner;
class Main{
public static void main (String args[]){
Scanner input = new Scanner (System.in);
boolean win=true;
int digit = input.nextInt();
int integer = input.nextInt();
for(;win && integer>0;integer/=10)
win=(integer%10)==digit;
System.out.println((win)?"win":"nah");
}
}
if you prefer the whole first and then the single digit just swap the two nextInt
you could also delete the boolean, but you lose in readability
+ 1
Like this
import java.util.Scanner;
class Main{
public static void main (String args[]){
Scanner input = new Scanner (System.in);
int d = input.nextInt();
int i = input.nextInt();
for(;i%10==d && i>0;i/=10);
System.out.println((i>0)?"win":"nah");
}
}
0
obviously 0 is not allowed
do you need 0 allowed in the integer?