+ 2
Test case 5 still fail,why?
import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String nl=sc.nextLine(); boolean b=true; String yn=sc.nextLine(); String[]n=nl.split(" "); for(int i=0;i<n.length;i++){ if(n[i].charAt(i)==yn.charAt(0)){ System.out.println("Compare notes"); break; }else{ System.out.println("No such luck"); break; } } } }
8 Antworten
+ 2
I think you only test the first person. Because whatever the result of the test is, you break out of the loop after the first test.
+ 2
You can remove the else. It is false unless you find someone whose name starts with the same letter. Then it is true, Then you can use break. You never go back from true to false for this problem.
+ 1
I think you made an error in your if, shouldn't it be like:
if(n[i].charAt(0)==yn.charAt(0))
0
Thanks for you reply
but still fail
0
You have break in if and else blocks. It iterates once only.. And checks only for first letter of 2 inputs..
What is you task actually..? Add description..
0
Here is a task
You are grouped into groups for a project, and you are supposed to come up with as many famous scientists who have the same first letter of their name as you as possible.
Will you have to come up with the answers on your own, or is there somebody in your group that you can work with?
Task:
Determine if anyone in your group has the same first letter of their name as you.
Input Format:
A string of your group members' names separated by spaces, and then a string of your name.
Output Format:
A string that says 'Compare notes' if you have a name buddy, or 'No such luck' if you have to work on this alone.
Sample Input:
Becky Joan Fred Trey
Brad
Sample Output:
Compare notes
0
And I try this code but can't make it work
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String nl=sc.nextLine();
boolean b=false;
String yn=sc.nextLine();
String[]n=nl.split(" ");
for(int i=0;i<n.length;i++){
if(n[i].charAt(0)==yn.charAt(0)){ b=true;
}else{
b=false;
}
}
if(b){
System.out.println("Compare notes");
}else{
System.out.println("No such luck");
}
}
}
0
import java.util.Scanner;
class Rectangle {
int length;
int breadth;
int area;
int perimeter;
void input() {
Scanner in = new Scanner(System.in);
System.out.print("Enter length of rectangle: ");
length = in.nextInt();
System.out.print("Enter breadth of rectangle: ");
breadth = in.nextInt();
}
void calculate() {
area = length * breadth;
perimeter = 2 * (length + breadth);
}
void display() {
System.out.println("Area of Rectangle = " + area);
System.out.println("Perimeter of Rectangle = " + perimeter);
}
public static void main(String args[]) {
Rectangle obj = new Rectangle();
obj.input();
obj.calculate();
obj.display();
}
}