Respuesta :
Answer:
The correct program to this question can be given as:
Program:
//import package.
import java.util.Scanner; //scanner package.
public class CheckingPasscodes //define class.
{
//all the code run here.
public static void main (String [] args) //define main method
{
Scanner scnr = new Scanner(System.in); //create Scanner class Object
boolean hasDigit; //define variable.
String passCode; //define variable.
hasDigit = false; //assign value.
passCode = scnr.next(); //taking user input.
int let0 = passCode.charAt(0); //assign value in integer variable
int let1 = passCode.charAt(1); //assign value in integer variable
int let2 = passCode.charAt(2); //assign value in integer variable
//conditional statement.
if ((Character.isDigit(let0)||Character.isDigit(let1)||Character.isDigit(let2))) //if block
{
hasDigit = true; //assign value.
}
if (hasDigit) //if block
{
System.out.println("Has a digit."); //message.
}
else //else block
{
System.out.println("Has no digit."); //message.
}
}
}
Output:
AB1
Has a digit.
Explanation:
In the given question there are many mistakes so we provide the correct solution to this question that can be described as follows:
- In above java program firstly, we import a package that is used for taking user input. Then we define a class that is "CheckingPasscodes" in this class we define the main method. In the main method, we create a scanner class object and define some variables that are "passCode and hasDigit". A variable passCode is used for user input.
- Then we define three integer variable that is "let0, let1, and let2". This variable uses a charAt() function that holds the index values and assign to these variables.
- Then we define conditional statements. In if block we check variables "let0, let1 and let2" value is equal to the number. so, we change the bool variable value that is "true".
- In second if block we check bool variable "hasDigit" value is true. so we print "Has a digit.". else we print "Has no digit.".
To make corrections to your code, implies that we debug the code. The following are the reasons why your code is not working
- Use of undeclared variables userInput, let0, let1 and let2
- Unbalanced parenthesis ( )
- Unbalance curly braces { }
The corrections to your code are:
- Declare let0, let1 and let2 as character variables
- Replace userInput with passCode.
- Balance all parentheses and curly braces
The complete correct code is as follows:
import java.util.Scanner;
public class CheckingPasscodes {
public static void main (String [] args) {
Scanner scnr = new Scanner(System.in);
boolean hasDigit;
String passCode;
hasDigit = false;
passCode = scnr.next();
char let0 = passCode.charAt(0);
char let1 = passCode.charAt(1);
char let2 = passCode.charAt(2);
if (Character.isDigit(let0) || Character.isDigit(let1) || Character.isDigit(let2)){
hasDigit = true;}
if (hasDigit) {
System.out.println("Has a digit.");}
else {System.out.println("Has no digit.");}
}
}
Read more about debugging at:
https://brainly.com/question/23527739