我一直试图编写一个使用while循环返回输入的第一个和最后一个数字的程序。
这是我到目前为止所得到的:
import java.util.Scanner;
public class FirstLastInt {
public static void main(String[]args)
{
int fdig, ldig, num;
Scanner in = new Scanner(System.in);
System.out.println("Enter an Integer.");
num = in.nextInt();
ldig = num%10;
while (num > 0)
{ fdig = num/10;
num = num/10;
System.out.println("The first digit is " + fdig + ", and the last digit is " + ldig + ".");
}
}
num是输入的整数,fdig是第一个数字,ldig是最后一个数字。
我认为最后一位数字将始终为num%10,并且我实现了到目前为止使用while循环,就像它写的将返回第一个数字......以及一堆其他数字我不是真的需要。
例如:
run:
Enter an Integer.
98884
The first digit is 9888, and the last digit is 4.
The first digit is 988, and the last digit is 4.
The first digit is 98, and the last digit is 4.
The first digit is 9, and the last digit is 4.
The first digit is 0, and the last digit is 4.
BUILD SUCCESSFUL (total time: 3 seconds)
看起来循环在我得到我想要的数字之前和之后运行。无论如何要指定我喜欢打印哪条线? (在示例中,它是"第一个数字是9,最后一个数字是4.")
答案 0 :(得分:6)
如果您想继续使用自己的代码来查看如何以这种方式实现,那么请更改您的while条件并在外面进行系统打印:
import java.util.Scanner;
public class FirstLastInt {
public static void main (String[] args) {
int ldig, num;
Scanner in = new Scanner(System.in);
System.out.println("Enter an Integer.");
num = in.nextInt();
ldig = num % 10;
// keep looping num until it is in the range of 0-9
while (num >= 10) {
num = num / 10;
}
//initialize fdig variable and set it equal to num, just to make things clearer
int fdig = num;
System.out.println("The first digit is " + fdig + ", and the last digit is " + ldig + ".");
}
}
答案 1 :(得分:1)
以下是一种使用Scanner in = new Scanner(System.in);
System.out.println("Enter an Integer.");
int num = in.nextInt();
String numString = String.valueOf(num);
char first = numString.charAt(0);
char last = numString.charAt(numString.length() - 1);
System.out.println("The first digit is " + first + ", and the last digit is " + last);
并且不需要循环的方法:
<Storyboard x:Key="OnMouseLeftButtonDown1">
<PointAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransformOrigin)" Storyboard.TargetName="DailySpinColoursText_png">
<EasingPointKeyFrame KeyTime="0" Value="0.5,0.5"/>
<EasingPointKeyFrame KeyTime="0:0:0.3" Value="0.5,0.5"/>
<EasingPointKeyFrame KeyTime="0:0:0.6" Value="0.5,0.5"/>
<EasingPointKeyFrame KeyTime="0:0:1" Value="0.5,0.5"/>
<EasingPointKeyFrame KeyTime="0:0:1.4" Value="0.5,0.5"/>
<EasingPointKeyFrame KeyTime="0:0:1.8" Value="0.5,0.5"/>
<EasingPointKeyFrame KeyTime="0:0:2.2" Value="0.5,0.5"/>
<EasingPointKeyFrame KeyTime="0:0:2.6" Value="0.5,0.5"/>
<EasingPointKeyFrame KeyTime="0:0:3" Value="0.5,0.5"/>
<EasingPointKeyFrame KeyTime="0:0:3.4" Value="0.5,0.5"/>
<EasingPointKeyFrame KeyTime="0:0:3.8" Value="0.5,0.5"/>
<EasingPointKeyFrame KeyTime="0:0:4.2" Value="0.5,0.5"/>
<EasingPointKeyFrame KeyTime="0:0:4.6" Value="0.5,0.5"/>
<EasingPointKeyFrame KeyTime="0:0:5" Value="0.5,0.5"/>
</PointAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)" Storyboard.TargetName="DailySpinColoursText_png">
<EasingDoubleKeyFrame KeyTime="0" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="100"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.6" Value="400"/>
<EasingDoubleKeyFrame KeyTime="0:0:1" Value="1100"/>
<EasingDoubleKeyFrame KeyTime="0:0:1.4" Value="2600"/>
<EasingDoubleKeyFrame KeyTime="0:0:1.8" Value="4600"/>
<EasingDoubleKeyFrame KeyTime="0:0:2.2" Value="6100"/>
<EasingDoubleKeyFrame KeyTime="0:0:2.6" Value="7300"/>
<EasingDoubleKeyFrame KeyTime="0:0:3" Value="8300"/>
<EasingDoubleKeyFrame KeyTime="0:0:3.4" Value="8800"/>
<EasingDoubleKeyFrame KeyTime="0:0:3.8" Value="9000"/>
<EasingDoubleKeyFrame KeyTime="0:0:4.2" Value="9100"/>
<EasingDoubleKeyFrame KeyTime="0:0:4.6" Value="9150"/>
<EasingDoubleKeyFrame KeyTime="0:0:5" Value="9200"/>
<EasingDoubleKeyFrame KeyTime="0:0:5.4" Value="9220"/>
<EasingDoubleKeyFrame KeyTime="0:0:5.8" Value="9225"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
答案 2 :(得分:1)
package com.chandan;
公共类主要{
public static void main(String[] args) {
System.out.println(sumoffirstandlast(78548));
}
public static int sumoffirstandlast(int number){
int sum=0;
if(number<0){
return -1;
}
if(number<10){
return number+number;
}
int first=number%10;
while(number>0)
{
number=number/10;
if(number<10)
{
int digit=number%10;
sum=sum+digit;
}
}
int sum1=sum+first;
return sum1;
}
}
答案 3 :(得分:0)
import java.util.Scanner;
class q2_prototype3
{
public static void main()
{
Scanner scan = new Scanner(System.in);
System.out.print("Please enter a number: ");
double number = scan.nextDouble();
char firstDigit = String.valueOf(number).charAt(0);
double lastDigit = number%10;
System.out.println("Number given: " + number );
System.out.println("First digit: " + firstDigit);
System.out.println("Last digit: " + lastDigit );
}
}