标签: printf

在Java中的方法参数中使用printf

我有一个名为CustomerTypeApp的主类.它有一个类变量private static float itemPrice = 0.0f.在main方法中是以下代码:

String promptType = ("  To determine a discount for " + nf.format((float) itemPrice) + ", type in the... ...Select =>  ");
char customer = Validation.getChar(promptType);
Run Code Online (Sandbox Code Playgroud)

我还有一个名为Validation的类,其方法是getChar.代码如下:

public static char getChar(String prompt){
    String input;
    char choice;
    System.out.printf("  " + prompt);
    input = sc.nextLine();
    choice = input.charAt(0);
    return choice;
}
Run Code Online (Sandbox Code Playgroud)

在CustomerTypeApp类中,nf.format显然是指NumberFormat.但是,教授说我们不能使用NumberFormat而是必须使用printf.getChar方法确实使用了printf语句.但是,我无法传递printf(变量?)的值.这就是我的意思:我编辑了CustomerTypeApp的代码,如下所示:

String promptType = ("  To determine a discount for $%.2s, type in the... ...Select => ");
Run Code Online (Sandbox Code Playgroud)

问题是我必须通过itemPrice传递$%.2s.我尝试将它添加到promptType的末尾,如下所示:

("  To determine...$%.2s...Select => ", itemPrice); …
Run Code Online (Sandbox Code Playgroud)

java printf

0
推荐指数
1
解决办法
115
查看次数

printf(),unsigned char和%hhd

该程序:

#include <stdio.h>
#include <limits.h>

int main( void )
{
#ifdef __CHAR_UNSIGNED__
    printf( "%d\n", __CHAR_UNSIGNED__ );
#endif
    printf( "%d\n", CHAR_MAX );
    printf( "%d\n", CHAR_MIN );
    printf( "%hhd\n", CHAR_MAX );
}
Run Code Online (Sandbox Code Playgroud)

输出(在我的x86_64桌面上):

127
-128
127
Run Code Online (Sandbox Code Playgroud)

这是预期的.现在,我在Raspberry Pi(ARM)上运行了相同的操作:

1
255
0
-1
Run Code Online (Sandbox Code Playgroud)

所以... 显然我误解了一些步骤CHAR_MAX正在采取输出的方式,因为在最后一行预期的输出 - 传递CHAR_MAX%hhd一个char没有签名的机器- 本来就是255.

(如果你应该问,结果是相同的(char)CHAR_MAX,和(unsigned char)CHAR_MAX.)

我错过了什么?

在回归测试我自己printf()在Raspberry Pi上的实现时发生了这种情况- 顺便说一句,它会打印出来255.所以...只有一个实施正确,我有下沉的感觉,这不是我的......

c printf

0
推荐指数
1
解决办法
668
查看次数

Perl的printf宽度修饰符在map运算符中不起作用

为什么,当我做map{printf "%4s\n",File::Spec->rel2abs($_)}glob '*';print map{sprintf "%4s\n",File::Spec->rel2abs($_)}glob '*';4个空格字符输出到字符串的左边?...

/var/www/html/Physics/Electronics/file1.txt
/var/www/html/Physics/Electronics/file2.txt
/var/www/html/Physics/Electronics/file3.txt
/var/www/html/Physics/Electronics/file4.txt
Run Code Online (Sandbox Code Playgroud)

似乎我必须做map{print "\x{20}\x{20}\x{20}\x{20}",File::Spec->rel2abs($_),"\n"}glob '*';的是输出字符串左边的4个空格字符...

    /var/www/html/Physics/Electronics/file1.txt
    /var/www/html/Physics/Electronics/file2.txt
    /var/www/html/Physics/Electronics/file3.txt
    /var/www/html/Physics/Electronics/file4.txt
Run Code Online (Sandbox Code Playgroud)

perl printf

0
推荐指数
1
解决办法
40
查看次数

主{}括号上的语法错误,"期望的时间"?

我完全难过了.无论我做什么,我的编译器都认为我需要在上一次printf中使用while循环?它只是一直在寻找没有意义的事情.当我将它们添加到其中时只是告诉我它不应该存在,当我删除它时我被告知我需要它.

int main() {
float userInput; //The grades that the user inputs.
int passingGrade = 0; //Number of passing grades
int failingGrade = 0; //Failing grades
int invalidGrade = 0; //Invalid grades

//This do for loop is simply counting the number of grades of each catagory posted.
do {
    printf("Enter a grade (Enter -1 to quit):");
    scanf("%f", &userInput);
    printf("You entered: %.1f\n", userInput);
    if ((userInput > 100) || (userInput < 0)) { //Over 100 is impossible.
        invalidGrade = invalidGrade + 1; …
Run Code Online (Sandbox Code Playgroud)

c printf while-loop do-while

0
推荐指数
1
解决办法
71
查看次数

x =(char*)&a的作用是什么?它有什么作用?

我有这段代码:

int a;
char *x;
x = (char *) &a;
a = 512;
x[0] = 1;
x[1] = 2;
printf("%d\n");
return 0;
Run Code Online (Sandbox Code Playgroud)

这打印513.请解释,尤其是第3行.

c printf pointers char

0
推荐指数
1
解决办法
700
查看次数

C中的printf行为

我试图了解以下内容之间的区别:

printf("%f",4.567f);
printf("%f",4.567);
Run Code Online (Sandbox Code Playgroud)

如何使用f后缀更改/影响输出?

c printf

0
推荐指数
1
解决办法
54
查看次数

printf(..)参数的执行顺序是什么?

我想做这样的事情printf("%d,%d",array[c], array[c+1]),只有一个变量在每次迭代中都会增加.我写了下面的代码,我期待"1,2"到stdout,我错了:

#include <stdio.h>
int main()
{
    int c = 1;
    printf("%d,%d",c++,c++); /* 2,1 */
    //printf("%d,%d",c,c++);   /* 2,1 */
    //printf("%d,%d",c++,c);   /* 1,2 */
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试做printf("%d,%d",c++,c)printf("%d,%d",c,c++),我可以看到,有没有定义的"C++"语句的执行顺序.

有人可以解释我写的代码是如何编译的吗?为什么以及如何根据"c ++"语句的位置进行更改.

c printf

0
推荐指数
1
解决办法
80
查看次数

从结构中打印通过C中的值传递

TL; DR:

printf()printLotInfo()通过值时打印垃圾.

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <math.h>
#include <string.h>

typedef struct Time
{
   int hour;                              //Hour of day
   int minute;                            //Minute of hour
} Time;

typedef struct Car
{
   char * plateNumber;                    //String to hold plate
   char hasPermit;                        //True/False
   Time * enteringTime;                   //Time Struct
   int lotParkedIn;                       //Where is the car located
} Car;

typedef struct ParkingLot
{
   int lotNumber;                         //Lot identifier
   double hourlyRate;                     //$$/h
   double maxCharge;                      //Maximum daily charge
   int capacity;                          //How many …
Run Code Online (Sandbox Code Playgroud)

c printing printf struct

0
推荐指数
1
解决办法
81
查看次数

Unexpected behaviour of printf function in c

I recently encountered with an interview question. I did not understand the behaviour of printf function in this case

 #include <stdio.h>
 int main() {
 int k = printf("String");
 printf("%d",k);
 }
Run Code Online (Sandbox Code Playgroud)

Expected result : Compilation Error

Output : String6

Why is the output String6?

c printf

0
推荐指数
1
解决办法
53
查看次数

创建并打开FIFO后,fprintf()到stdout无效

当我的程序启动时,它只是创建一个fifo并将其打开,之后我只想在屏幕上输出一些信息,但是什么也没打印出来。这是我的代码片段:

void listen(const server_config_t* server_conf)
{
    // Create FIFO
    if (mkfifo(SERVER_FIFO_PATH, 0660) == -1) {
        fprintf(stdout, "server FIFO not created as it already exists. continuing...\n");
    }

    // Open FIFO (for reading)
    int fd;
    if ((fd = open(SERVER_FIFO_PATH, O_RDONLY)) == -1) {
        // fprintf(stderr, "error: could not open server FIFO\n");
        perror("FIFO");
        exit(1);
    }

    // Open dummy FIFO (for writing, prevent busy waiting)
    // TODO: find way to wait without another file descriptor?
    int fd_dummy;
    if ((fd_dummy = open(SERVER_FIFO_PATH, O_WRONLY)) == -1) …
Run Code Online (Sandbox Code Playgroud)

c printf stdout fflush

0
推荐指数
1
解决办法
67
查看次数

标签 统计

printf ×10

c ×8

char ×1

do-while ×1

fflush ×1

java ×1

perl ×1

pointers ×1

printing ×1

stdout ×1

struct ×1

while-loop ×1