-5
public class ldigit
{
     public static void main( String args[])
     { 
        int a;
        int lastdigit;
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        a = n;     
        while( n>10 )
        {
            a = a / 10;          
        }
        lastdigit = n % 10;

        System.out.println("firstdigit" + a );
        System.out.println("last digit" + lastdigit);
      }
} 
4

3 回答 3

5

我假设未问的问题是“为什么这段代码不起作用”。

您最终可能会在a. while将您的线路更改为

while( a>=10 )

让它工作。

于 2013-07-20T11:40:18.160 回答
3
while (n > 10) {
    a = a / 10;          
}

您在这里有一个无限循环,因为您永远不会n在循环内进行修改。

我看不出有任何理由有两个变量an.

旁注:看看代码在 Maroun Maroun 格式化后变得更加清晰易读。努力以完美的方式格式化您的代码。您将花费数小时查看代码:您最好使其易于阅读。

于 2013-07-20T11:37:03.833 回答
0

嘿,你的 While 循环是错误的,这样做

while(a>10)
{
    a=a/10;
 }
  firsdigit=a;
  lastdigit=n%10;
于 2013-07-20T11:58:09.360 回答