【发布时间】:2017-03-09 08:26:47
【问题描述】:
嗯,我的老师给了我一个java作业。他想要一个分析句子的程序。我写了代码,但它不起作用。我做错了什么你能检查一下吗? (嗯,我花了很多时间自学java并编写这些代码,所以这对我来说意义重大!)
对于页面 Ödev1.java ==>
package Ödev1;
import javax.swing.JOptionPane;
public class Ödev1 {
public static void main(String[] args) {
String input = JOptionPane.showInputDialog("Enter a sentence");
System.out.print(input);
odev1 cumle = new odev1();
cumle.analiz();
}
}
对于页面 odev1.java ==>
package odev1;
public class odev1 {
String input;
int characters;
int words;
int gaps;
int lowerletters;
int capitalletters;
int vowels;
int sibilancies;
public odev1(int characters, int words, int gaps, int lowerletters, int capitalletters, int vowels, int sibilancies) {
this.characters = characters;
this.words = words;
this.gaps = gaps;
this.lowerletters = lowerletters;
this.capitalletters = capitalletters;
this.vowels = vowels;
this.sibilancies = sibilancies;
}
public void analysis()
{
characters=input.length();
char[] array = input.toCharArray();
int n=0;
for(int a=0;a<characters;a++) {
do{
n=n+1;
}while(array[a]!='0'); }
words=characters-n;
gaps=words-1;
capitalletters=0;
lowerletters=0;
for(int b=0;b<characters;b++) {
if (Character.isUpperCase(input.charAt(b))) capitalletters++;
if (Character.isLowerCase(input.charAt(b))) lowerletters++; }
vowels=0;
for(int c=0; c< input.length();c++) {
switch(input.charAt(c)) {
case 'a':
case 'e':
case 'ı':
case 'i':
case 'o':
case 'ö':
case 'u':
case 'ü':
vowel++;
break; }
}
sibilancies=characters-gaps-vowels;
System.out.println("Count of characters="+characters);
System.out.println("Count of words="+words);
System.out.println("Count of gaps="+gaps);
System.out.println("Count of capital letters="+capitalletters);
System.out.println("Count of lower letters="+lowerletters);
System.out.println("Count of vowels="+vowels);
System.out.println("Count of sibilancies="+sibilancies);
}
}
【问题讨论】:
-
您显然知道如何将代码复制/粘贴到问题中,因此请为所有相关代码这样做。不要插入指向代码截图的链接。 --- 提示: 修正代码的缩进,使其可读。并且在粘贴到问题时,额外缩进 4 个空格,以便代码在此处正确格式化。
-
这个地方太多了,我的代码是可读的,你为什么这么想?
-
我不知道“有太多地方可以做”是什么意思。 “太少”可能是有道理的,除了有足够的空间来缩进你的代码。缩进代码以显示代码的 结构 使其更具可读性且不易出错。这就是为什么我(和大多数其他人)那样想的原因。至于屏幕截图的链接,它只是不好/懒惰,如果链接服务器关闭时问题变得毫无意义。此外,“它不起作用”是对您的问题的糟糕描述!!!!
-
扩展/解释@Andreas 所评论的内容。请对代码和代码 sn-ps、结构化文档(如 HTML/XML 或输入/输出)使用代码格式。为此,选择文本并单击消息发布/编辑表单顶部的
{}按钮。 -
@Andreas “不要插入指向代码截图的链接。” 这么好的建议,我认为值得重复,.. 这次在粗体。