我不能在动态场景中使用制表符空间而不是列宽说明符

cant I use tab spaces instead of column width specifier in dynamic scenario

提问人:AsderSynth 提问时间:10/24/2023 最后编辑:khelwoodAsderSynth 更新时间:10/24/2023 访问量:33

问:

import java.util.Scanner;

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    // Get the employee name and basic salary
    System.out.println("Enter the employee name:");
    String name = scanner.nextLine();
    System.out.println("Enter the basic salary:");
    double basicSalary = scanner.nextDouble();

    // Calculate the DA and SA based on the basic salary
    double da = 0;
    double sa = 0;
    if (basicSalary <= 10000) {
        da = basicSalary * 0.1;
        sa = basicSalary * 0.05;
    } else if (basicSalary <= 20000) {
        da = basicSalary * 0.12;
        sa = basicSalary * 0.08;
    } else if (basicSalary <= 30000) {
        da = basicSalary * 0.15;
        sa = basicSalary * 0.1;
    } else {
        da = basicSalary * 0.2;
        sa = basicSalary * 0.12;
    }

    // Calculate the gross salary
    double grossSalary = basicSalary + da + sa;

    // Display the employee salary information in tabular format
    System.out.println();
    System.out.printf("%-20s%-15s%-15s%-20s%-15s%n", "Name", "Basic", "DA", "Spl. Allowance", "Gross Salary");
    System.out.printf("%-20s%-15.2f%-15.2f%-20.2f%-15.2f%n", name, basicSalary, da, sa, grossSalary);
}

在上面的代码中,有没有办法以相同的格式显示,但使用简单的逻辑,如“/t”?

我尝试只输入“/t”,但在某些情况下不起作用。 此外,在这方面不允许使用字符串函数。

Java 字符串 if-statement

评论

3赞 k314159 10/24/2023
我不确定我是否理解你的问题,但也许你正在寻找 - 不是.是制表符,而只是两个单独的字符和 。\t/t\t/t/t
2赞 aled 10/24/2023
使用制表符来间隔是个坏主意。可能不是每个系统都一样工作。你使用显式间距所做的是正确的想法。不过,它可以以不同的方式实现。
0赞 Community 10/25/2023
请澄清您的具体问题或提供其他详细信息,以准确说明您的需求。正如目前所写的那样,很难确切地说出你在问什么。

答: 暂无答案