java拉丁正方形_java – 猪拉丁程序,切换/否则错误

//我需要帮助我的其他if语句.如果单词以元音开头,则添加到单词末尾的方式.如果这个单词以辅音开头,那么你可以将辅音放在最后并添加一个.

我的问题是,如果我的第一个字母带有元音一个字,它就像有一个辅音一样贯穿其中.如果我输入“是”,我会得到“arewayreaay”而不是“areway”.

public class piglatin {

public static void main(String[] args) {

String str = IO. readString();

String answer = "";

if(str.startsWith("a"))

System.out.print(str + "way");

if(str.startsWith("e"))

System.out.print(str + "way");

if(str.startsWith("i"))

System.out.print(str + "way");

if(str.startsWith("o"))

System.out.print(str + "way");

if(str.startsWith("u"))

System.out.print(str + "way");

else{

char i = str.charAt(0);

answer = str.substring( 1, str.length());

System.out.print(answer + i + "ay");

}

}

}

解决方法:

if(str.startsWith("a") || str.startsWith("e") || str.startsWith("i") ... (and so on)) {

System.out.print(str + "way");

} else{

char i = str.charAt(0);

answer = str.substring( 1, str.length());

System.out.print(answer + i + "ay");

}

或者if / else if / else

说明:

// combine all the if blocks together .. if you dont it checks for vowel 'a' and prints

// 'areay' then it checks for vowel 'u' and enters the else block .. where it again

// operates on 'are' (check immutability of strings) gets charAt(0) i.e 'a' and prints

// 'rea' and since you concatenated 'ay' ... the final output = 'arewayreaay'

标签:java,io,apache-pig,string

来源: https://codeday.me/bug/20190718/1494559.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值