2017-02-22 39 views
-5
import java.util.Scanner; 
public class Bigger{ 

public static void main(String [] args) 
{ 
// declare variables 
Scanner keyboardIn = new Scanner(System.in); 
String userName = new String(); 
String fName = new String(); 
int numberLetters = 0; 
int bigLetters=0; 
char firstLetter; 

// get user name from the user 
System.out.print("Please enter your user name: "); 
userName = keyboardIn.nextLine(); 

// get second name from the user 
System.out.print("Please enter your second name: "); 

fName = keyboardIn.nextLine(); 

// use an appropriate method to find the number of letters 
numberLetters = userName.length(); 
bigLetters = fName.length(); 

if(numberLetters > bigLetters) 
{ 
    System.out.print("String 1 Is the longest string "); 
} 
else 
{ 
    System.out.print("String 2 Is the longest string "); 
} 
} 
} 

我需要這個打印出實際字母的實際字符串,所以如果丹麥是更大的字符串我需要它打印出來給用戶。我該怎麼做呢?需要此返回輸入內容

問候,

馬克

+0

開始。 – bejado

+2

這不是代碼審查的地方。如果你有關於代碼的具體問題,那麼你需要明確地問它。否則,人們不太可能爲你調試你的代碼。 – Encaitar

+0

因此......'System.out.print(userName +「是最長的字符串」);'? –

回答

0

你應該居然還要檢查,看看是否輸入的字符串的長度相等(以下如果需要,可以縮短檢查大於或等於使用>=或小於或等於使用到<=):

import java.util.Scanner; 

class Bigger { 
    public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 

    // get user name from the user 
    System.out.print("Please enter your user name: "); 
    String userName = input.nextLine(); 
    // get second name from the user 
    System.out.print("Please enter your second name: "); 
    String secondName = input.nextLine(); 

    // use an appropriate method to find the number of letters and prompt user 
    if(userName.length() == secondName.length()) { 
     System.out.println(userName + " is equal in length than " + secondName); 
    } else if(userName.length() > secondName.length()) { 
     System.out.println(userName + " is longer in length than " + secondName); 
    } else { 
     System.out.println(userName + " is shorter in length than " + secondName); 
    } 
    } 
} 

實例應用:

Please enter your user name: MarkDoherty 
Please enter your second name: Denmark 
MarkDoherty is longer in length than Denmark 

另外,您可以使用字符串格式化像這樣:

// use an appropriate method to find the number of letters 
if(userName.length() == secondName.length()) { 
    System.out.printf("%s (%d characters long) is equal in length than %s (%d characters long)\n", userName, userName.length(), secondName, secondName.length()); 
} else if(userName.length() > secondName.length()) { 
    System.out.printf("%s (%d characters long) is longer in length than %s (%d characters long)\n", userName, userName.length(), secondName, secondName.length()); 
} else { 
    System.out.printf("%s (%d characters long) is shorter in length than %s (%d characters long)\n", userName, userName.length(), secondName, secondName.length()); 
} 

實例應用:

Please enter your user name: MarkDoherty 
Please enter your second name: Denmark 
MarkDoherty (11 characters long) is longer in length than Denmark (7 characters long) 

告訴我們您遇到什麼問題試試看here!

0

如果我正確認識你,最簡單的方法是隻串連,你需要用變量打印出System.out.print()內的任何字符串。舉例來說,如果我有一個名爲String類型的變量myString我會使用的代碼行:

System.out.print("This is my string variable" + myString); 

而且,除非你想在同一行的一切一定要附加換行符到您的字符串或結束使用System.out.println()

+0

感謝您的幫助。你們都非常有幫助。 –

+0

我的問題得到了解答。我只需要程序接受用戶的兩個名字,然後打印出最長的名字。我只能得到它顯示哪個變量名稱最長,所以它只是說變量1更大或變量2更大。我希望它能夠返回最大的變量和該變量的內容。我會在下次提問時多加小心,我知道你們都非常敏感。 –