问题描述
- 问个简单的(12小时转至24小时制):这个哪里错了?
-
#include"iostream"
using namespace std;
void input(int &hours24,int &minutes)
{
cin>>hours24>>":">>minutes;
}
void convert(int&hours,char&M)
{
if(hours-12<0)AMPM='A';
else
{
hours-=12;
AMPM='P';
}
}
void output(int hours,int minutes,char AMPM)
{
cout<<hours<<":"<<minutes<<AMPM<<endl;
}
void main()
{
int hours24,minutes;
char AMPM;
input(&hours24,&minutes);
convert(&hours24,&M);
output(hours24,minutes, AMPM);
}
解决方案
#include<iostream>
using namespace std;
void input(int &hours24,int &minutes)
{
char tmp;
cin>>hours24>>tmp>>minutes;
}
void convert(int&hours,char&M)
{
if(hours-12<0)AMPM='A';
else
{
hours-=12;
AMPM='P';
}
}
void output(int hours,int minutes,char AMPM)
{
cout<<hours<<":"<<minutes<<AMPM<<endl;
}
int main()
{
int hours24,minutes;
char AMPM;
input(hours24,minutes);
convert(hours24,AMPM);
output(hours24,minutes, AMPM);
}
解决方案二:
SimpleDateFormat的12小时制和24小时制
SimpleDateFormat的12小时制和24小时制
DateFormatUtils 24小时制和12小时制
时间: 2024-09-19 11:46:36