Toto je kód, ktorý v súčasnosti musím zreťaziť a potom b, potom c a tak ďalej v slučke (naskenovaný počet krát) pomocou javy:
public String toString()
{
String answers = "";
int numChoices = choices.length;
char letter;
String result;
int letterNum = 0061;
while (numChoices > 0)
{
letter = "u" + letterNum;
result = letter + ") " + choices[choices.length-numChoices] + "n";
answers += result;
numChoices --;
letterNum ++;
}
return question + "n" + answers;
}
Myslel som, že únikové sekvencie unicode budú mojou najlepšou voľbou, ale nefunguje tak, ako som sa snažil, takže zjavne robím niečo zlé. Ako to vyriešim?
Chyba, ktorú dostávam, je:
MultChoice.java:40: illegal unicode escape
letter = "u" + letterNum;
odpovede:
1 pre odpoveď č. 1Úniky Unicode sú spracovávané javacom, veľmi čoskoro v kompilácii, pred analýzou. Kompilátor nikdy neuvidí uniknutie Unicode, iba kódové body. Preto ich nemôžete používať za behu. Skúste to takto:
public String toString()
{
String answers = "";
int numChoices = choices.length;
char letter = "a";
String result;
while (numChoices > 0)
{
result = "" + letter + ") " + choices[choices.length-numChoices] + "n";
answers += result;
numChoices --;
letter ++;
}
return question + "n" + answers;
}
A char
je iba 16-bitové celé číslo bez znamienka, takže s ním môžete robiť všetky bežné celé čísla, napríklad prírastky. Nie je potrebné samostatné int
--"a"
a (char) 0x61
sú to isté.
0 pre odpoveď č. 2
Hodnota letterNum
je 49 (61 v osmičkovej sústave), takže sa zmení na "u49"
, ktorý nie je platný.
Možno ste mali používať 0x0061
, a potom to otočte na a String
použitím Integer.toHexString(letterNum)
.
Edit: Zdá sa, že nemôžete vytvoriť String
použitím "u" + something
.
Možný spôsob je Character.toString((char) letterNum)
.